0

所以我有自定义UITableViewCells(用故事板制作),当我没有从字面上分配,初始化单元格时,应用程序崩溃了。但是,由于它们是自定义单元,我不知道如何分配/初始化它们。

此代码崩溃:

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

    TableViewCells *cell = (TableViewCells *)[tableView dequeueReusableCellWithIdentifier:@"TableViewCellsID"];

return cell;
}

将此打印到控制台:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

我知道这意味着它崩溃了,因为还没有分配单元格,但是当我尝试分配它时,我必须指定一个单元格样式。

因此,当我按如下方式构建单元时,它不会崩溃:

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

     TableViewCells *cell = (TableViewCells *)[tableView  dequeueReusableCellWithIdentifier:@"TableViewCellsID"];
     if (cell == nil)
          cell = [[TableViewCells alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TableViewCellsID"];

     return cell;
}

问题是有效的代码(第二组),需要我设置一个UITableViewStyle. 据我所知,没有 UITableViewStyleCustom。

任何帮助将不胜感激。

4

1 回答 1

-1

它在第一种情况下崩溃,因为没有单元可重用,而您正在调用dequeueReusableCellWithIdentifier.

编辑 - 对于自定义单元初始化,您可以检查以下线程 -

如何在 3.0 之后使用 initWithStyle 制作自定义 TableViewCell

initWithFrame 与 initWithStyle

于 2012-06-28T15:51:13.953 回答