7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TableViewControllerのサブクラスつくったら’unable to dequeue a cell with identifier Cell’という実行時エラーが出た。

Last updated at Posted at 2013-10-13

Xcode5.0を使ってSubclassをTableViewControllerに指定して自動で生成したファイルをビルドしたら、シミュレーションでの起動で実行時エラーがでました。(xibは不使用)

二回躓いたので、解決方法をメモします。

出力されたエラーメッセージ

erminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'  

エラーとなったメソッド

(UITableViewCell *)tableView:(UITableView |*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

解決方法

自動で生成される以下のコードを変更します。

変更前

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

変更後

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

セルの生成時に、forIndexPath:indexPathを渡さないよう変更しました。
変更後のtableView:cellForRowAtIndexPath:が以下になります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if(cell == nil){
          cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
     }
     
    //cellへの表示する内容を設定する
    
    return cell;
}
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?