【问题标题】:UITableView with .xib cell with error : failed to obtain a cell from its dataSourceUITableView with .xib cell with error : failed to get a cell from its dataSource
【发布时间】:2017-03-11 19:16:41
【问题描述】:

我已经用 .xib 文件创建了表格单元格。并使用以下代码添加到 UITableView 中:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DesplayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"desplayCell"];

    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"DisplayCell" bundle:nil] forCellReuseIdentifier:@"desplayCell"];
    }

   //
 return cell;
}

应用程序因错误而崩溃:

未能从其数据源获取单元格

【问题讨论】:

  • 确保故事板或 XIB 文件中的单元格具有相同的标识符 desplayCell
  • 是的,两者都是一样的。

标签: ios objective-c iphone uitableview


【解决方案1】:

我忘记在您的代码中添加dequeueReusableCellWithIdentifier

dequeueReusableCellWithIdentifier 用法

您应该对相同表单的所有单元格使用相同的重用标识符。 重用标识符与表格视图的那些单元格(行)相关联,这些单元格(行)具有相同的常规配置,但减去单元格内容。

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DesplayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"desplayCell"];

    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"DisplayCell" bundle:nil] forCellReuseIdentifier:@"desplayCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"desplayCell"];
    }

   //
 return cell;

}

【讨论】:

  • 您应该在“viewdidload”中注册单元格并制作一个可重复使用的单元格 dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath。如下函数为指定的重用标识符返回一个可重用的表格视图单元格对象并将其添加到表格中。
【解决方案2】:

你可以这样使用

在 viewDidLoad 中写下代码

  [tableView registerNib:[UINib nibWithNibName:@"DisplayCell" bundle:nil] forCellReuseIdentifier:@"desplayCell"];

在 cellForRowAtIndexPath 你可以写在下面

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        DesplayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"desplayCell"];

          if (cell == nil) { // if cell is nil then you can alloc it

       cell=[[DesplayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"desplayCell"];

            cell = [tableView dequeueReusableCellWithIdentifier:@"desplayCell"];

        }

 return cell;

}

【讨论】:

    【解决方案3】:

    答案就像Siddhesh Mhatrepost 但是你应该做一个UITableView的类别,它对大型项目很有用。

    UITableView+Category.h

    - (id)dequeueReusableOrRegisterCellWithIdentifier:(NSString *)identifier;
    - (id)dequeueReusableOrRegisterCellWithClass:(Class)aClass;
    

    UITableView+Category.m

    - (UITableViewCell *)dequeueReusableOrRegisterCellWithIdentifier:(NSString *)identifier{
        UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:identifier];
    
        if (!cell) {
            [self registerNib:[UINib nibWithNibName:identifier bundle:nil] forCellReuseIdentifier:identifier];
            cell = [self dequeueReusableCellWithIdentifier:identifier];
        }
    
        return cell;
    }
    
    - (UITableViewCell *)dequeueReusableOrRegisterCellWithClass:(Class)aClass{
        UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:NSStringFromClass(aClass)];
    
        if (!cell) {
            [self registerClass:aClass forCellReuseIdentifier:NSStringFromClass(aClass)];
            cell = [self dequeueReusableCellWithIdentifier:NSStringFromClass(aClass)];
        }
    
        return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-12
      • 2017-04-07
      • 2022-10-06
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 2022-12-27
      • 2012-08-14
      相关资源
      最近更新 更多