所以我有自定义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。
任何帮助将不胜感激。