UITableView数据源必须从tableView:cellForRowAtIndexPath/断言失败消息返回单元格

UITableView数据源必须从tableView:cellForRowAtIndexPath/断言失败消息返回单元格,uitableview,Uitableview,使用自定义UITableViewCell实现UITableView,代码如下 @interface ProfileSaveViewController : UITableViewController { UITableViewCell *cell0; UITableViewCell *cell1; UITableViewCell *cell2; UILabel *cell2Label; }

使用自定义UITableViewCell实现UITableView,代码如下

    @interface ProfileSaveViewController : UITableViewController
     {
         UITableViewCell *cell0;
         UITableViewCell *cell1;
         UITableViewCell *cell2;
         UILabel *cell2Label;
     }

    @property (nonatomic, retain) IBOutlet UITableViewCell *cell0;
    @property (nonatomic, retain) IBOutlet UITableViewCell *cell1;
    @property (nonatomic, retain) IBOutlet UITableViewCell *cell2;
    @property (nonatomic, retain) IBOutlet UILabel *cell2Label;

    @end

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:               (NSIndexPath*)indexPath
  {
        if([indexPath row] == 0) return cell0;
        if([indexPath row] == 1) return cell1;
        if([indexPath row] == 2) return cell2;   
        return nil;
  }
运行应用程序时出现以下消息

*在-[UITableView\u createPreparedCellForGlobalRow:withIndexPath:]、/SourceCache/UIKit\u Sim/UIKit-1912.3/UITableView.m:6072中断言失败
2012-03-01 11:11:31.984 TestSplitView[765:f803]*
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView数据源必须从tableView返回一个单元格:cellForRowAtIndexPath:”

有人要求您输入一个单元格,并返回
nil
(或者其他不是
UITableViewCell
)的东西。就这么简单

我认为有两种可能性:

  • 您正在从
    表视图:numberofrowsinssection:
    返回一个大于3的数字。请勿

  • 一个或多个
    单元格#
    插座未连接到nib,或未连接到
    UITableViewCell
    (或子类)。请正确连接它们


这不是创建TableView的方法。 您只需要学习UITableView类文档


如果您使用的是自定义UITableViewCell,也可能是您忘记在“实用程序端”->“属性检查器”选项卡上标识“标识符”


希望这有帮助。

您应该检查tableview数据源方法中的行数。请确保

/* Dynamic Prototype cell in UITableView */

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView ==tblUser) {
        switch (indexPath.row)
        {
            case 0:
                return Cell_0;
                break;
            case 1:
                return Cell_1;
                break;
            case 2:
                return Cell_2;
                break;
            case 3:
                return Cell_3;
                break;
            case 4:
                return Cell_4;
                break;
            case 5:
                return Cell_04;
                break;
            case 6:
                return Cell_5;
                break;
            case 7:
                return Cell_6;
                break;
            case 8:
                return Cell_7;
                break;
            default:
                return nil;
                break;
        }
    }else
return nil
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return 3;
}

如果返回的值大于此值,则cellForRow将返回nil,而tableview数据源需要UITableViewCell实例,因为该断言失败。

如果有少量单元格,则创建一组静态表视图单元格完全可以。事实上,该技术在标题下的中进行了描述“静态行内容的技术”。(NSInteger)tableView:(UITableView*)tableView numberofrowsinssection:(NSInteger)section{return 3;}则是另一种可能性。您还没有将所有插座连接到表视图单元格。您是指“连接”吗?”通过设置引用插座属性?是。在nib中,需要将表视图控制器的
cell1
cell2
cell3
插座连接到
UITableViewCell
的三个独立实例。