UITableView 之所以強大,很大一部分來自於可以自由自訂 UITableViewCell。一般來說,Cell 是動態產生的:使用過程中會建立一個 Cell 池,並依每個 Cell 的高度(即 tableView:heightForRowAtIndexPath: 的回傳值)與螢幕高度來計算螢幕內可顯示幾個 Cell。自訂 TableViewCell 大致分為純程式碼與透過 IB 編輯 nib 兩種方式,本文主要整理以程式碼實作各種自訂。
如何動態調整 Cell 高度
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *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 Separator 分割線
一般來說,用類似 [tableView setSeparatorColor:[UIColor redColor]]; 的語句即可修改 Cell 之間分割線的顏色。那要如何改用一張圖片作為分割線背景呢?可以嘗試以下方法:
方法一:
先把 Cell 的 separatorColor 設為 clear,再把做好的分割線圖片加到自訂的 custom cell 上。
方法二:
在 Cell 裡加入一個 1 像素的 imageView 並載入圖片,之後設定 tableView.separatorStyle = UITableViewCellSeparatorStyleNone。
自訂首行 Cell 與其上方導覽欄間距
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;
//這裡加入自己的邏輯
}