UITableViewCell 除了主文字,還預留了幾個擴充位置,其中最常用的就是附屬視圖(accessoryView)。系統內建的樣式只有那幾種箭頭與勾選框,若都不合用,我們完全可以塞入自訂控件;再加上 indentationLevel 與 indentationWidth 這兩個屬性,還能做出層級遞進的縮進效果,讓列表呈現樹狀結構的視覺層次。本文用一個十行的列表同時示範這兩件事。
ZYUITableViewController.h:
#import <UIKit/UIKit.h> @interface ZYUITableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> @property(nonatomic,strong) UITableView *myTableView; @end
ZYUITableViewController.m:
@synthesize myTableView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.myTableView.delegate = self;
myTableView.dataSource = self;
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:myTableView];
}
// 每行高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat result = 20.0f;
if ([tableView isEqual:self.myTableView]) {
result = 80.0f; // 加高以便容納附屬視圖
}
return result;
}
// 預設只有一個 Section,共十行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
// 每行的數據與附屬視圖
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
if ([tableView isEqual:myTableView]) {
static NSString *tableViewCellIdentifier = @"MyCells";
result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
if (result == nil) {
result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:tableViewCellIdentifier];
}
result.textLabel.text = [NSString stringWithFormat:@"Cell %ld", (long)indexPath.row];
// 建立自訂附屬視圖
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0.0f, 0.0f, 150.0f, 25.0f);
[button setTitle:[NSString stringWithFormat:@"Expand %ld", (long)indexPath.row]
forState:UIControlStateNormal];
[button addTarget:self action:@selector(performExpand:) forControlEvents:UIControlEventTouchUpInside];
result.accessoryView = button;
// 縮進:等級用行號,每級寬 10 點
result.indentationLevel = indexPath.row;
result.indentationWidth = 10.0f;
}
return result;
}
// 附屬按鈕被點擊
-(void)performExpand:(UIButton *)param{
NSLog(@"U pressed %@", param.titleLabel.text);
}
// 點擊某一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView isEqual:myTableView]) {
NSLog(@"%@", [NSString stringWithFormat:@"Cell %ld is selected", (long)indexPath.row]);
}
}
// 點擊系統附屬按鈕(若使用 accessoryType 時才會觸發)
-(void)tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *owenCell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"Cell Title = %@", owenCell.textLabel.text);
}
這裡有兩條容易混淆的路徑需要釐清。使用 accessoryView 時,事件由你自己加入的控件負責處理,也就是 performExpand:;而 accessoryButtonTappedForRowWithIndexPath: 只有在採用 accessoryType(系統內建的箭頭、勾選等)時才會被呼叫。範例中兩者並列,實際專案依需求擇一即可。
縮進部分,indentationLevel 是「縮進幾級」,indentationWidth 是「每級多少點」,最終偏移量為兩者相乘。因此若 indentationLevel 設為 0,即使 width 再大也不會有任何位移。
可以看到每一行的附屬按鈕都正常顯示,且隨著行號增加逐級向右遞移,形成清楚的層級感。
點擊第一行,控制台輸出:
2013-04-28 10:56:21.187 UITableViewTest1[877:c07] Cell 0 is selected
點擊第二行的附屬按鈕,控制台輸出:
2013-04-28 10:56:23.957 UITableViewTest1[877:c07] U pressed Expand 1