2012-06-28 52 views
0

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

此代码崩溃:

- (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。

任何帮助,将不胜感激。

回答

-1

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

编辑 - 对于自定义单元格的初始化,您可以检查以下主题 -

How to make custom TableViewCell with initWithStyle after 3.0

initWithFrame vs initWithStyle

+0

我明白为什么它的崩溃,但我需要一种方式来创建一个单元,而不指定样式 – Andrew

+0

为什么会投票?在代码中提到的问题是崩溃,我提供了原因。现在为第二个查询我会给出答案。 – rishi

+0

如果你对自定义单元格alloc感兴趣,那么你可以检查这个线程 - http://stackoverflow.com/questions/1754036/how-to-make-custom-tableviewcell-with-initwithstyle-after-3-0 – rishi

相关问题