開發中常會遇到需要在 table view 的某個 cell 裡再放一個 table view(或 collection view)的需求,例如「可展開的明細列表」。做法並不難,關鍵在於兩個 table view 共用同一個 delegate,因此每個回呼方法都必須先判斷來源是哪一個。
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
UITableView *rootTable;
UITableView *insertTabelView;
NSMutableArray *dataArray;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self initView];
dataArray = [[NSMutableArray alloc] initWithObjects:@"中", @"夢", @"科", @"技",
@"made in china", nil];
self.navigationItem.title = @"TwoTableView";
}
-(void)initView
{
rootTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 65,
self.view.frame.size.width, self.view.frame.size.height)
style:UITableViewStyleGrouped];
rootTable.delegate = self;
rootTable.dataSource = self;
[self.view addSubview:rootTable];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == rootTable) {
if (indexPath.row == 0) {
return [dataArray count] * 44; // 第一列要放內嵌表格,高度要足夠
} else {
return 70;
}
}
return 44;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return (tableView == rootTable) ? 5 : 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (tableView == rootTable) ? 4 : [dataArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] init];
if (tableView == rootTable) {
if (indexPath.row == 0) {
insertTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width, [dataArray count] * 44)];
insertTabelView.delegate = self;
insertTabelView.dataSource = self;
insertTabelView.scrollEnabled = NO; // 關閉內層捲動,交給外層
[cell.contentView addSubview:insertTabelView];
} else {
cell.textLabel.text = @"rootTableView";
}
return cell;
}
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor yellowColor];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == rootTable) {
NSLog(@"rootTableView");
} else {
NSLog(@"%@", [dataArray objectAtIndex:indexPath.row]);
}
}
@end
如此一來,內嵌的 insertTabelView 就被放進了外層的第一個 cell 中。
1. 內層必須關閉捲動(scrollEnabled = NO)。巢狀的兩個可捲動視圖會爭奪手勢,導致滑動時分不清要滾哪一層。關掉內層、讓整體由外層 scroll view 統一處理,是這個模式能否用的前提。
2. 外層那一列的高度必須手動算對。範例用 [dataArray count] * 44 回傳高度,若算錯就會出現內容被裁切或留白。資料量固定時可以這樣寫,若會變動,則需在高度方法中即時計算。
3. 不要在 cellForRow 中反覆建立內層 table view。範例為求簡潔直接在 cellForRow 中 new 了一個,但 cell 會被重用,這會導致不斷配置新物件、效能低落且狀態遺失。正確做法是把內層 table view 做成 cell 的 subview(自訂 UITableViewCell 子類別),只在建立時配置一次,之後每次只需 reloadData。
4. 共用 delegate 時務必分流。所有 dataSource 與 delegate 方法都要以 tableView == rootTable 判斷,否則外層與內層會互相干擾,出現「點了內層卻刷新外層」這類怪現象。若 delegate 邏輯複雜,替內層指定一個獨立的 dataSource 物件會清楚得多。
如果內層是「橫向捲動的卡片列表」或需要多欄排列,UICollectionView 會比巢狀 table view 合適。iOS 上更乾淨的替代方案是使用 UITableView 搭配自訂 cell 內嵌 collection view,或直接在 iOS 13 之後改用 UICollectionViewCompositionalLayout,它原生支援「群組內再分群組」的巢狀結構,不必真的做視圖巢狀,能避開上述大部分手勢與重用問題。