多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 互聯網 > 自定義UITableViewCell:Cell高度、分割線、間距等

自定義UITableViewCell:Cell高度、分割線、間距等

來源:程序員人生   發布時間:2014-10-08 08:00:00 閱讀次數:2668次

UITableView的強大更多程度上來自于可以任意自定義UITableViewCell單元格。

通常,UITableView中的Cell是 動態的,在使用過程中,會創建一個Cell池,根據每個cell的高度(即tableView:heightForRowAtIndexPath:返回 值),以及屏幕高度計算屏幕中可顯示幾個cell。而進行自定義TableViewCell無非是采用代碼實現或采用IB編輯nib文件來實現兩種方式, 本文主要收集代碼的方式實現各種cell自定義。

如何動態調整Cell高度

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  

    staticNSString*CellIdentifier=@"Cell";  

    UITableViewCell*cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  

    if(cell ==nil){  

        cell =[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];  

        UILabel*label =[[UILabel alloc] initWithFrame:CGRectZero];  

        label.tag =1;  

        label.lineBreakMode =UILineBreakModeWordWrap;  

        label.highlightedTextColor =[UIColor whiteColor];  

        label.numberOfLines =0;  

        label.opaque = NO;// 選中Opaque表示視圖后面的任何內容都不應該繪制  

        label.backgroundColor =[UIColor clearColor];  

        [cell.contentView addSubview:label];  

        [label release];  

    }  

   

    UILabel*label =(UILabel*)[cell viewWithTag:1];  

    NSString*text;  

    text =[textArray objectAtIndex:indexPath.row];  

    CGRect cellFrame =[cell frame];  

    cellFrame.origin =CGPointMake(0,0);  

   

    label.text = text;  

    CGRect rect =CGRectInset(cellFrame,2,2);  

    label.frame = rect;  

    [label sizeToFit];  

    if(label.frame.size.height >46){  

        cellFrame.size.height =50+ label.frame.size.height -46;  

    }  

    else{  

        cellFrame.size.height =50;  

    }  

    [cell setFrame:cellFrame];  

   

    return cell;  

}  

 

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath  

{  

    UITableViewCell*cell =[self tableView:tableView cellForRowAtIndexPath:indexPath];  

    return cell.frame.size.height;  

}  

 

如何用圖片自定義Table Separeator分割線
一般地,利用類似[tableView setSeparatorColor:[UIColor redColor]];語句即可修改cell中間分割線的顏色。那又如何用一個圖片作為分割線背景呢?可以嘗試如下:
方法一:
先設置cell separatorColor為clear,然后把圖片做的分割線添加到自定義的custom cell上。

方法二:
在cell里添加一個像素的imageView后將圖片載入進,之后設置tableView.separatorStyle = UITableViewCellSeparatorStyleNone

自定義首行Cell與其上面導航欄間距

  1. tableView.tableHeaderView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,5,20)] autorelease];  

自定義UITableViewCell的accessory樣式
      默認的accessoryType屬性有四種取值:UITableViewCellAccessoryNone、 UITableViewCellAccessoryDisclosureIndicator、 UITableViewCellAccessoryDetailDisclosureButton、 UITableViewCellAccessoryCheckmark。

如果想使用自定義附件按鈕的其他樣式,則需使用UITableView的accessoryView屬性來指定

UIButton*button;  

if(isEditableOrNot){  

    UIImage*image =[UIImage imageNamed:@"delete.png"];  

    button =[UIButton buttonWithType:UIButtonTypeCustom];  

    CGRect frame =CGRectMake(0.0,0.0,image.size.width,image.size.height);  

    button.frame = frame;  

    [button setBackgroundImage:image forState:UIControlStateNormal];  

    button.backgroundColor =[UIColor clearColor];  

    cell.accessoryView = button;  

}else{  

    button =[UIButton buttonWithType:UIButtonTypeCustom];  

    button.backgroundColor =[UIColor clearColor];  

    cell.accessoryView = button;  

}  

 

以上代碼僅僅是定義了附件按鈕兩種狀態下的樣式,問題是現在這個自定義附件按鈕的事件仍不可用。

即事件還無法傳遞到 UITableViewDelegate的accessoryButtonTappedForRowWithIndexPath方法上。

當我們在上述代碼 中在加入以下語句:
       [button addTarget:self action:@selector(btnClicked:event:) forControlEvents:UIControlEventTouchUpInside];
后, 雖然可以捕捉到每個附件按鈕的點擊事件,但我們還無法進行區別到底是哪一行的附件按鈕發生了點擊動作!因為addTarget:方法最多允許傳遞兩個參 數:target和event,這兩個參數都有各自的用途了(target指向事件委托對象,event指向所發生的事件)??磥碇灰揽緾ocoa框架已 經無法做到了。

      但我們還是可以利用event參數,在自定義的btnClicked方法中判斷出事件發生在UITableView的哪一個cell上。因為UITableView有一個很關鍵的方法indexPathForRowAtPoint,可以根據觸摸發生的位置,返回觸摸發生在哪一個cell的indexPath。而且通過event對象,正好也可以獲得每個觸摸在視圖中的位置。

 

// 檢查用戶點擊按鈕時的位置,并轉發事件到對應的accessory tapped事件  

-(void)btnClicked:(id)sender event:(id)event

{

  NSSet*touches =[event allTouches];

  UITouch*touch =[touches anyObject];

  CGPoint currentTouchPosition =[touch locationInView:self.tableView];

  NSIndexPath*indexPath =[self.tableView indexPathForRowAtPoint:currentTouchPosition];

  if(indexPath !=nil)

        {

             [self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];

         }

}

 

這樣,UITableView的accessoryButtonTappedForRowWithIndexPath方法會被觸發,并且獲得一個indexPath參數。通過這個indexPath參數,我們即可區分到底哪一行的附件按鈕發生了觸摸事件。

 

-(void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath  

{

     int*idx = indexPath.row;

     //這里加入自己的邏輯  

}

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲精品456在在线播放 | 国产视频久久 | 国产精品亚洲欧美大片在线看 | 亚洲精品久久久久中文 | 日韩一级片免费 | 天天综合色一区二区三区 | 午夜视频在线网站 | 黄色avav| 国产精品福利在线观看秒播 | 国产在线啪| 91免费福利视频 | 国产亚洲欧洲国产综合一区 | 国产精品成人免费综合 | 国产免费看网站v片不遮挡 国产免费全部免费观看 | 国产v日韩v欧美v精品专区 | 国产不卡免费视频 | 欧美抽搐一进一进一出 | 纯欧美一级毛片_免费 | 波多野结衣 在线资源观看 波多野结衣 一区二区 | 精品免费国产一区二区三区 | 99久久精品国产综合一区 | 日本在线不卡一区二区 | 国产成视频 | 欧美激情αv一区二区三区 欧美激情第二页 | 欧美另类老人xxxx | 亚洲在线一区二区三区 | 亚洲欧美一区二区久久 | 精品一区二区三区亚洲 | 久久伊人五月天 | 最近中文字幕无吗免费版 | 亚洲成a人片在线观看中文动漫 | 另类ts人妖一区二区三区 | 亚洲中字| 日本www视频 | 五月天最新网站 | 一二三四视频社区在线中文 | 亚欧乱色 | 国产片在线 | 久久精品国产视频在热 | 欧美成人免费全部观看天天性色 | 亚洲成年网站 |