我已将UITableViewCell
子类化为MyCell
并尝试将表格显示出来。我总是收到消息:
exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
tableView:cellForRowAtIndexPath
的代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
MyCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
MyCell* theCell = (MyCell *)cell;
UILabel* lab = theCell.theLabel;
lab.text = @"This is the label text";
return cell;
}
我在笔尖中将CellIdentifier设置为“Cell”。我以为我要用最后一行返回一个单元格? 我哪里错了?
答案 0 :(得分:1)
你的细胞必须是零。你应该白了:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
MyCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[NSBundle mainBundle] loadNibNamed:@"MyCell"].lastObject;
}
// Configure the cell...
UILabel* lab = cell.theLabel;
lab.text = @"This is the label text";
return cell;
}