【问题标题】:UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath / Assertion failure messagesUITableView dataSource 必须从 tableView 返回一个单元格:cellForRowAtIndexPath / 断言失败消息
【发布时间】:2012-03-19 15:46:17
【问题描述】:

使用自定义 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 _createPreparedCellForGlobalRow:withIndexPath:] 中的断言失败,/SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:6072 2012-03-01 11:11:31.984 TestSplitView[765:f803] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView 数据源必须从 tableView:cellForRowAtIndexPath: 返回一个单元格:”

【问题讨论】:

    标签: uitableview


    【解决方案1】:

    您被要求输入一个单元格并返回 nil(或者可能不是 UITableViewCell 的其他内容)。就是这么简单。

    我看到了两种可能性:

    • 您从 tableView:numberOfRowsInSection: 返回一个大于 3 的数字。不要。

    • 您的一个或多个cell# 插座未连接到您的笔尖,或未连接到UITableViewCell(或子类)。正确连接它们。

    【讨论】:

    • - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 3; }
    • 那么这是另一种可能性。您还没有将所有插座都连接到表格视图单元格。
    • 您的意思是通过设置引用出口属性来“挂钩”吗?
    • 是的。在笔尖中,您需要将表格视图控制器的cell1cell2cell3 出口连接到UITableViewCell 的三个独立实例。
    【解决方案2】:

    如果您使用自定义 UITableViewCell,也可能是您忘记在“Utilites side”->“Attribute Inspector”选项卡上识别“Identifier”。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      【讨论】:

      • 如果单元格数量很少,创建一组静态的表格视图单元格完全没问题。
      • 事实上,该技术在 Table View Programming Guide for iOS 中的“静态行内容技术”标题下进行了描述。
      【解决方案4】:
      /* 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
      }
      

      【讨论】:

        【解决方案5】:

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

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
           return 3;
        }
        

        如果您返回的不止于此,那么您的 cellForRow 将返回 nil,由于该断言失败,该 tableview 数据源需要 UITableViewCell 实例。

        【讨论】:

          猜你喜欢
          • 2011-10-09
          • 2013-09-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多