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;
}

問題は、動作するコード (2 番目のセット) であり、UITableViewStyle. 私の知る限り、UITableViewStyleCustom はありません。

どんな助けでもいただければ幸いです。

4

1 に答える 1

-1

再利用するセルがなく、 を呼び出しているため、最初のケースではクラッシュしますdequeueReusableCellWithIdentifier

編集-カスタムセルの初期化については、次のスレッドを確認できます-

3.0 以降の initWithStyle でカスタム TableViewCell を作成する方法

initWithFrame と initWithStyle の比較

于 2012-06-28T15:51:13.953 に答える