*** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-3347.44.2/UITableView.m:6245
- (void)setupTableView
{
self.tableView.tableHeaderView = self.header;
// 執行該行代碼時,會執行 tableView: cellForRowAtIndexPath:方法,最終崩潰在tableView dequeueReusableCellWithIdentifier:kMyCustomCellId1 forIndexPath:indexPath, 以后盡早注冊tableView的cell
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self registerCells]; // 把該代碼放在 [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];之前就能解決此問題
}
- (void)registerCells
{
[self.tableView registerClass:[MyCustomCell1 class] forCellReuseIdentifier:kMyCustomCellId1];
[self.tableView registerClass:[MyCustomCell2 class] forCellReuseIdentifier:kMyCustomCellId2];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyBaseCustomCell *cell;
if (indexPath.item % 2 == 0) {
// cell = [tableView dequeueReusableCellWithIdentifier:kMyCustomCellId1];
cell = (MyCustomCell1 *)[tableView dequeueReusableCellWithIdentifier:kMyCustomCellId1 forIndexPath:indexPath];
} else {
// cell = [tableView dequeueReusableCellWithIdentifier:kMyCustomCellId2];
cell = (MyCustomCell2 *)[tableView dequeueReusableCellWithIdentifier:kMyCustomCellId2 forIndexPath:indexPath];
}
return cell;
}
另外一個坑點,如果 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 返回的是Nil, 則會包這個錯,例如如果用這個 cell = [tableView dequeueReusableCellWithIdentifier:kMyCustomCellId1];實現數據源方法,則會報此錯誤
*** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-3347.44.2/UITableView.m:7524