来自IB的UITableViewCell将值显示为NULL

时间:2012-07-25 18:59:29

标签: iphone ios ios5 uitableview

以下内容用作将nib文件中的tableviewcell放入tableview的代码。我把tableviewcell放在与tableview相同的nib中。我已经尝试在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单元格时,它变为null。

3 个答案:

答案 0 :(得分:1)

你需要这样做

static NSString *CellIdenetifier = @"Cell";

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

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

我不确定你是否需要,但我认为我把它包括在内。

答案 1 :(得分: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;

答案 2 :(得分:1)

当然是空的。

如果表视图需要显示的队列中有更多的单元格,则从

返回nil
dequeueReusableCellWithIdentifier:

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

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

return cell;

啊,ps:忘掉Interface Builder。

相关问题