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

UITableView的強大更多程度上來自於能夠任意自定義UITableViewCell單元格。app

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

如何動態調整Cell高度框架

 

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

    staticNSString*CellIdentifier=@"Cell";  code

    UITableViewCell*cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  orm

    if(cell ==nil){  對象

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

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

        label.tag =1;  圖片

        label.lineBreakMode =UILineBreakModeWordWrap;  get

        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指向所發生的事件)。看來只依靠Cocoa框架已 經沒法作到了。

      但咱們仍是能夠利用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;

     //這裏加入本身的邏輯  

}

相關文章
相關標籤/搜索