0

UITableViewCellasをサブクラス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」として設定しました。最後の行でセルを返していると思いましたか? どこが間違っていますか?

4

1 に答える 1

1

あなたのセルはnilでなければなりません。次のようなものを白くする必要があります。

- (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;
}
于 2012-12-18T20:52:39.433 に答える