【问题标题】:UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: ExceptionUITableView 数据源必须从 tableView 返回一个单元格:cellForRowAtIndexPath:异常
【发布时间】:2010-02-08 19:37:21
【问题描述】:

我实际上没有看到我的错误:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";

  FriendTableViewCell *cell = (FriendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
      cell = [[[FriendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      [[NSBundle mainBundle] loadNibNamed:@"FriendTableViewCell" owner:self options:nil];
      cell = friendCell;
  }
  cell.lblNickname.text =  @"Tester";
  return cell;
}

我做错了什么?我检查了两次.. 但没有看到错误。

感谢您的帮助!

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    你正在返回friendCell,它很可能是nil。

    您的代码看起来不错,因此请确保您的 Interface Builder 文件设置正确。在 FriendTableViewCell.xib 中,确保 File's Owner 是您的表格视图控制器,并且您正确地将单元格设置为friendCell(我假设是 UITableViewCell)的出口。

    【讨论】:

    • 谢谢,再次检查后,我发现 Interface Builder 中的插座连接丢失!
    • 谢谢,您的回答正是我所需要的。我更改了视图控制器类的名称,在没有考虑的情况下中断了与 IB 的连接。不要再改名字了,因为我不喜欢他们!
    • 很好的答案,通常问题在于返回一个零单元格!
    • 如果您在 UIViewController 中有 UITableView,请确保您已创建从 UITableView 到 UIViewController 的出口连接。
    【解决方案2】:

    对我来说,这样做很有效:

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    

    更新
    将上面的代码放在如下方法中:

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

    只是把它放在之前编辑tableViewCell

    【讨论】:

      【解决方案3】:

      您正在创建一个FriendTableViewCell,然后忽略它并将其设置为(可能)等于名为friendCell 的实例变量。

      我假设您希望在调用 loadNibNamed 方法时设置friendCell。显然没有设置。

      所以这段代码有两个问题。首先,不要两次分配一个单元格。

      cell = [[[FriendTableViewCell ....
      [[NSBundle mainBundle .....
      cell = friendCell;
      

      显然,如果您用第二次调用分配给单元格来覆盖它,那么创建一个新单元格并将其分配给单元格是没有用的。

      其次,friendCell 可能为零。确保 NIB 设置正确并且插座指向正确的位置。

      【讨论】:

      • 感谢您的帮助! friendCell 是我的 Outlet,如何正确设置?
      【解决方案4】:

      我知道这是一篇旧帖子,但是我刚刚遇到了这个错误,我发现它很奇怪,因为该应用程序正在测试中,所以几天没有新的构建,它做到了,我所做的只是重新启动电话解决了。

      【讨论】:

        【解决方案5】:

        我发现在初始化我的表格视图之前尝试创建我的 UITableViewCell 时出现了这个问题:

        这里在创建tableView之前注册类会报错,放在之后会修复报错。

        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:bCellIdentifier]; 
        
        tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        tableView.delegate = self;
        tableView.dataSource = self;
        
        [self addSubview:tableView];
        
        tableView.keepInsets.equal = KeepRequired(0);
        

        【讨论】:

        • 在我的情况下,我调用该函数在表格 UITableViewCell 注册之前加载数据,赞成你的回答帮助我克服这个问题
        【解决方案6】:

        看这里:Loading TableViewCell from NIB

        这是 Apple 针对该主题的文档。

        //This is assuming you have tvCell in your .h file as a property and IBOutlet
        //like so:
        TableViewController.h
        @property(nonatomic,retain) IBOutlet UITableViewCell *tvCell;
        //Data Source Method...
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        
        if (cell == nil) {
        
            [[NSBundle mainBundle] loadNibNamed:@"TVCell" owner:self options:nil];
        
            cell = tvCell;
        
            self.tvCell = nil;
        

        使用 loadNibNamed:owner:options 方法在笔尖中加载单元格。将单元格实例设置为您的 nib 对象,然后将 nib 对象设置为 nil。

        阅读我链接的其余文档,了解如何访问单元格内的子视图。

        【讨论】:

          【解决方案7】:

          不要忘记在 Interface Builder 中设置 cell identifier

          【讨论】:

            【解决方案8】:

            使用这个,

            UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];
            

            仅此几行,我又节省了几个小时。

            【讨论】:

              【解决方案9】:

              确保您的原型单元的 NIB / Storyboard 文件中的重用标识符与您称为 CellIdentifier 的任何内容匹配

               static NSString *CellIdentifier = @"Cell";
              

              【讨论】:

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