在自定义单元格中的标签中分配值时出现异常

时间:2011-05-18 12:41:58

标签: iphone

我有一个表格视图,我从不同的笔尖加载自定义单元格,我得到例外:

 2011-05-18 18:01:00.323 custInfoService[4370:20b] *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit/UIKit-984.38/UITableView.m:4709
2011-05-18 18:01:00.324 custInfoService[4370:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
2011-05-18 18:01:00.325 custInfoService[4370:20b] Stack: (
    11125851,
    2442997307,
 

当我使用断点时,我才知道xception是在switch case中。我无法弄清楚为什么它会给出异常?帮我! 这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
        [[NSBundle mainBundle] loadNibNamed:@"BookDetailViewController" owner:self options:NULL];
        cell = nibloadedcell;
       }

    tbcel.layer.cornerRadius = 10;
    tbcel.text = aBook.Name;


    UILabel *fieldlabel = (UILabel *) [cell viewWithTag:1];
    fieldlabel.text = [fieldarray objectAtIndex:(indexPath.row)];
    UILabel *valuelabel = (UILabel *) [cell viewWithTag:2];
    switch(indexPath.section)

    {
        case 0:
            valuelabel.text = aBook.Address;
            break;
        case 1:
            valuelabel.text = aBook.Phone;
            break;
        case 2:
             valuelabel.text = aBook.Purchase;
    }

    return cell;
}

1 个答案:

答案 0 :(得分:0)

所以最明显的问题是

[[NSBundle mainBundle] loadNibNamed:@"BookDetailViewController" owner:self options:NULL];
cell = nibloadedcell;

未设置您的nibloadedcell商家,因此cell始终为nil。因此,不会返回有效的单元格,并且您会收到错误UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

不要忘记,nil的消息会被默默忽略 - 如果单元格为nil,则函数的其余部分可以正常执行,并且不执行任何操作!