9

1 つのセクションと 3 つの行を持つ tableViewController があるプログラムがあります。アプリを実行するたびに、メソッドで常にクラッシュしdequeueReusableCellWithIdentifier:ます。このメソッドにブレークポイントを挿入しNSLog()てゼロにしました。Cell Identifierまた、ストーリーボードのプロトタイプ セルの を に設定しましたCell。しかし、プログラムはまだクラッシュします。問題を引き起こしている方法は次のとおりです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"indexPath.section = %d, indexPath.row = %d", indexPath.section, indexPath.row);
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil)
    {
        NSLog(@"in nil");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    return cell;
}

クラッシュログには次のように書かれています:

MyAppTake4[7463:707] indexPath.section = 0, indexPath.row = 0
2012-11-05 23:21:20.747 MyCardAppTake4[7463:707] -[UITableView     dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector sent to instance     0xfbc9000
2012-11-05 23:21:20.753 MyCardAppTake4[7463:707] *** Terminating app due to uncaught     exception 'NSInvalidArgumentException', reason: '-[UITableView     dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector sent to instance     0xfbc9000'
*** First throw call stack:
(0x3254788f 0x3459d259 0x3254aa9b 0x32549915 0x324a4650 0x7e2f7 0x31fceefb 0x31fcdfd9     0x31fcd763 0x31f71f15 0x324a61fb 0x3420aaa5 0x3420a6bd 0x3420e843 0x3420e57f 0x342064b9     0x3251bb1b 0x32519d57 0x3251a0b1 0x3249d4a5 0x3249d36d 0x315f4439 0x31f9ccd5 0x7d9f9 0x7d994)
terminate called throwing an exception(lldb)

この間違いを繰り返さないように、エラーの正確な原因を特定することに関心があります。

ご協力いただきありがとうございます。

4

5 に答える 5

9

[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; が原因です。は iOS 6 で追加されましたが、iOS 5 では認識されません。iOS 5 との互換性のために、代わりに以下の方法を使用します。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
于 2012-11-06T04:34:59.650 に答える
7

最初にセルまたは nib のクラスを登録しましたか?

アップルのドキュメントから:

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

(ところで、その方法はiOS 6でのみ利用可能です)

iOS 6 を使用している場合は、新しい単純なパラダイムを使用できます。

viewDidLoad で、クラスを登録します。あなたの場合、それは単なる UITableViewCell クラスです。

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

次に、 cellForRowAtIndexPath で次のようにすることができます。

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.myArray[indexPath.row];
    return result;
}
于 2012-11-06T04:35:38.030 に答える
1

forIndexPath メソッドは ios6 用であるため、このスニペットを使用することをお勧めします。以下を参照。

UITableViewCell *cell = [table_view dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

    // **initialize** your stuff here **with their respected tag**
}
else
{
   // **get** your stuff back using their tags
}
// **assign** your valiues here

return cell;

プログラミングを楽しむ

于 2012-11-06T04:39:28.713 に答える
0

このエラーの別の原因は次のとおりです-

無効な nib が識別子 (セル) に登録されました - nib には、UICollectionReusableView インスタンスである必要がある最上位オブジェクトが 1 つだけ含まれている必要があります

collectionViewCell ではなく、xib に UIView がありました。お役に立てれば。

于 2016-08-23T20:00:00.947 に答える
0

私はこの例外を抱えていましたが、それは私の愚かな間違いが原因であることに気付きました. 空白の .xib ファイルを作成しました (カスタムの "Loading" セルUITableViewView. どっ!

結果は、表示されている例外メッセージであり、出力ウィンドウには次のように表示されます。

2014-04-25 20:45:12.953 Languages[36256:60b] *** Terminating app due to uncaught 
exception 'NSInternalInconsistencyException', reason: 'invalid nib registered
for identifier (LoadingCell) - nib must contain exactly one top level object 
which must be a UITableViewCell instance'

もちろん、解決策はTable View Cell、.xib ファイルに を追加し、他の UI コンポーネントをそこにコピーし、そのテーブル ビュー セルをファイルの「ルート」要素として残すことでした。

于 2014-04-25T18:48:26.707 に答える