【问题标题】:exception while assigning values in label in custom cell在自定义单元格中的标签中分配值时出现异常
【发布时间】:2011-05-18 12:41:58
【问题描述】:

我有一个表格视图,我在其中从不同的笔尖加载了自定义单元格,但出现异常:

 2011-05-18 18:01:00.323 custInfoService[4370:20b] *** 断言失败 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit/UIKit-984.38/UITableView.m:4709
2011-05-18 18:01:00.324 custInfoService[4370:20b] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView 数据源必须从 tableView:cellForRowAtIndexPath 返回一个单元格:”
2011-05-18 18:01:00.325 custInfoService[4370:20b] 堆栈:(
    11125851,
    2442997307,
 

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 静态 NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 如果(单元格 == 零) { [[NSBundle mainBundle] loadNibNamed:@"BookDetailViewController" owner:self options:NULL]; 单元格 = 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]; 开关(indexPath.section) { 案例0: valuelabel.text = aBook.Address; 休息; 情况1: valuelabel.text = aBook.Phone; 休息; 案例2: valuelabel.text = aBook.Purchase; } 返回单元格; }

【问题讨论】:

    标签: iphone


    【解决方案1】:

    所以最明显的问题是

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

    没有设置您的nibloadedcell 插座,所以cell 一直是nil。因此没有返回有效单元格,您会收到错误UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

    别忘了,发给nil 的消息会被默默地忽略——如果单元格是nil,函数的其余部分可以正常执行,只是什么都不做!

    【讨论】:

    • 谢谢...我犯了一个愚蠢的错误,:) nib 名称是“bookdetailviewcontrollercell”,我使用的是上面我写的。
    • @user720235:如果他的回答解决了你的问题,你应该把它标记为已回答,这样他就可以得到信用,其他人也可以看到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多