0

以下被用作将表格视图单元从 nib 文件放入表格视图的代码。我将 tableviewcell 放在与 tableview 相同的笔尖中。我也尝试过在 viewdidload 中初始化单元格,但没有成功。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"identifier";
    NSLog(@"the cellidentifier is %@", CellIdentifier);
    bookmarksTableViewCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSLog(@"the cell is %@", bookmarksTableViewCell);
    return bookmarksTableViewCell;
}

我得到的错误是

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

我检查了 IB 中的连接并重新接线,但结果相同。当我尝试 NSLog'ing 单元格时,它为空。

4

3 回答 3

1

你需要这样做

static NSString *CellIdenetifier = @"Cell";

BookMarksTableViewCell *cell = (BookMarksTableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil){
   cell = [[BookMarksTableViewCell alloc] initWithStyle:UITableViewCellStyleSubTitle reuseIdentifer:CellIdentifier]; 
}

我不确定你是否需要它,但我想我包括它。

于 2012-07-25T19:06:47.767 回答
1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    static NSString *CellIdentifier = @"identifier";
    NSLog(@"the cellidentifier is %@", CellIdentifier);

    bookmarksTableViewCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (bookmarksTableViewCell == nil){
            //allocate your cell here
    }

    return bookmarksTableViewCell;
于 2012-07-25T19:06:54.140 回答
1

当然它是空的。

如果一个表格视图需要显示的单元格比其队列中可用的单元格多,它会从

dequeueReusableCellWithIdentifier:

要求数据源创建一个新单元,直到分配足够数量的单元。检查 NULL:

bookmarksTableViewCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (bookmarksTableViewCell == NULL)
    bookmarksTableViewCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStylePlain reuseIdentifier:cellIdentifier] autorelease];

return cell;

啊,ps:忘记Interface Builder。

于 2012-07-25T19:07:07.607 回答