dequeueReusableCellWithIdentifier:forIndexPath でのアサーション失敗: 質問する

dequeueReusableCellWithIdentifier:forIndexPath でのアサーション失敗: 質問する

そこで私は学校用の RSS リーダーを作っていて、コードを完成させました。テストを実行したところ、このエラーが発生しました。これが参照されているコードです:

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

        cell = 
         [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
                                reuseIdentifier:CellIdentifier];

    }

出力のエラーは次のとおりです。

2012-10-04 20:13:05.356 Reader[4390:c07] * -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]、/SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:4460 でアサーション エラーが発生しています 2012-10-04 20:13:05.357 Reader[4390:c07] *キャッチされていない例外 'NSInternalInconsistencyException' のため、アプリを終了しています。理由: '識別子 Cell のセルをデキューできません - 識別子の nib またはクラスを登録するか、ストーリーボードでプロトタイプ セルを接続する必要があります' *最初のスロー コール スタック: (0x1c91012 0x10cee7e 0x1c90e78 0xb64f35 0xc7d14 0x39ff 0xd0f4b 0xd101f 0xb980b 0xca19b 0x6692d 0x10e26b0 0x228dfc0 0x228233c 0x228deaf 0x1058cd 0x4e1a6 0x4ccbf 0x4cbd9 0x4be34 0x4bc6e 0x4ca29 0x4f922 0xf9fec 0x46bc4 0x47311 0x2cf3 0x137b7 0x13da7 0x14fab 0x26315 0x2724b 0x18cf8 0x1becdf9 0x1becad0 0x1c06bf5 0x1c06962 0x1c37bb6 0x1c36f44 0x1c36e1b 0x147da 0x1665c 0x2a02 0x2935) libc++abi.dylib: 例外をスローして終了が呼び出されました

エラー画面に表示されるコードは次のとおりです。

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

助けてください!

ベストアンサー1

メソッドを使用していますdequeueReusableCellWithIdentifier:forIndexPath:ドキュメンテーションその方法はこう言います:

重要:このメソッドを呼び出す前に、registerNib:forCellReuseIdentifier:またはメソッドを使用してクラスまたは nib ファイルを登録する必要があります。registerClass:forCellReuseIdentifier:

再利用識別子の nib またはクラスが登録されていません"Cell"

コードを見ると、nilセルがない場合には dequeue メソッドが戻ることを期待しているようです。dequeueReusableCellWithIdentifier:その動作には を使用する必要があります。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

dequeueReusableCellWithIdentifier:と は異なるメソッドであることに注意してくださいdequeueReusableCellWithIdentifier:forIndexPath:前者そして後者

を使用する理由を理解したいのであればdequeueReusableCellWithIdentifier:forIndexPath:このQ&Aをご覧ください

おすすめ記事