2012-06-28 51 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

相關問題