2013-04-02 9 views
13

私は次のエラーを取得する:UITableViewがロードしようとしたときにデキューできないというエラーが表示されるのはなぜですか?

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

は私が間違ってやっている正確に何かわかりません。私はセルの識別子を設定しています(プログラムでは、Interface Builderで作成されていないので)、デリゲートメソッドでやろうと思ったことはすべて実行しますが、UITableViewをロードしようとすると、

ここでは、関連するコード(それは私がカスタマイズオプションのためのUITableViewCellのサブクラス化しまし注目に値します)です:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     self.fontFamilyLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 20)]; 
     self.fontFamilyLabel.textAlignment = NSTextAlignmentCenter; 

     [self.contentView addSubview:self.fontFamilyLabel]; 
    } 
    return self; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return self.fonts.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"FontCell"; 

    FontCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 

    if (!cell) { 
     cell = [[FontCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FontCell"]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    int row = indexPath.row; 

    cell.fontFamilyLabel.text = self.fonts[row]; 

    return cell; 
} 

そして、ここでは、私は私のサブクラスのUITableViewCell(FontCell)に変更唯一の方法だが、

私は間違って何をしていますか?

+0

Identitifierを設定し、私はこのスレッドにそれを発見しました良い解決策があります:http:// stackoverflow。com/questions/12737860 /アサーション - デキュー可能なセルとインデックスパスを区別する –

答えて

28

最も簡単な修正は、現在のコードと同様に、FontCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];に変更することです。この方法を行う場合は、cellnilでないことを確認する必要があります。


代わりに、あなたは(viewDidLoad中まで)例えば@"FontCell"

に結び付けられているテーブルレベルでUINibまたはClassを登録することができます。

[self.tableView registerClass: [FontCell class] forCellReuseIdentifier:@"FontCell"]; 

次にあなたが

を行うことができます
FontCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FontCell" forIndexPath:indexPath]; 

この方法の素晴らしい点は、セルが決してnilにならないことを知っていることです。ただちに変更を開始することができます。

+0

各セルの識別子を変更するとどうなりますか?私はそれが別の方法がありますが動作するとは思わない? –

4

dequeueReusableCellWithIdentifier:forIndexPath:メソッドを使用しています。そのメソッドのドキュメントはこれを言う:

You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method. 

だからここ

[self.tableView registerClass: [FontCell class] forReuseIdentifier: @"FontCell"]; 
+1

おそらく、 'UITableViewCell'のサブクラスである' FontCell'クラスを登録する必要があります。 – Brian

+0

@Brianええとセル –

+0

セルがカスタムセルレジスタを使用するときは、それは[self.tableView registerClass:[FontCell class] forCellReuseIdentifier:@ "FontCell"] ; – Henry

2

私はまた、このような問題を抱えていた、と私が見つけた解決策は次のとおりです。

がプロジェクトナビゲータに移動し、選択して「 ViewController.h "を参照してください。 "UIViewController"の後に、

<UITableViewDelegate, UITableViewDataSource> 

の後に追加します。

+0

これはなんですか?それは何ですか? – CRDave

+0

私は今答えを編集しました。これをチェックして。 – offset

1

私が以前のようにテーブルビューコントローラ(テーブルビューコントローラではない)を使用している場合、デフォルトで表示されるセルはありません。ストーリーボードで

テーブルビュー オープン属性インスペクタを選択し、0から1 へ 変更プロトタイプ細胞が属性インスペクタで、新たに表示されたテーブルセル を選択しますが「FontCell」

関連する問題