0

各エントリの行が必要な NSDictionary が返されていますが、このセクションの上に 1 つのエントリしかないセクションも必要です。以下のコードでこれを実行しようとしていますが、移動するとエラーが発生し続けますif ステートメントを入力します。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // returns the number of rows per section based off each entry in letterDictionary
    NSString *currentLetter = [sectionLetterArray objectAtIndex:section];
    if (section == 1) {
        return 1;
    }
    else
    return [[letterDictionary objectForKey:currentLetter] count];
}

これは、テーブルビューの最初のセクションを設定しようとしたときに表示されるエラーです。

2012-08-20 16:12:11.983 kPad[5681:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(0x3686756b 0x36b4797f 0x367a856d 0x56c11 0x3793f8b7 0x37940c19 0x37940b6d 0x3794075d 0x5826d 0x57dfb 0x5569b 0x55ee9 0x5fd65 0x32d8bead 0x373f3ccb 0x5f6a5 0x61c99 0x5f531 0x5ef6f 0x5b443 0x374105b5 0x37361991 0x373618ad 0x32ccaacf 0x32cca1bb 0x32cf2323 0x367ae2e1 0x32cf2783 0x32c56c65 0x36838143 0x368379a9 0x368366ef 0x367b4c1d 0x367b4aa9 0x3967833b 0x3791e535 0x4edd5 0x3557fb20)
libc++abi.dylib: terminate called throwing an exception

問題は、いくつかのセクションが存在する可能性があり、それが動的であるということです..それが、if elseを使用する理由です..これが正しいことであることを願っています. エラーを受け取り続けているだけです。

4

3 に答える 3

0

問題は、sectionLetterArray のインデックスが 1 ではないため、クラッシュすることです。else ステートメントを削除して (既に何かを返しているため、セクションが 1 の場合は実行されません)、次のように置き換えます。NSString *currentLetter = [sectionLetterArray objectAtIndex:section];

編集:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // returns the number of rows per section based off each entry in letterDictionary

    if (section == 1) {
        return 1;
    }

    NSString *currentLetter = [sectionLetterArray objectAtIndex:section];
    return [[letterDictionary objectForKey:currentLetter] count];
}
于 2012-08-20T04:31:13.067 に答える
0

配列内のアイテムの数sectionLetterArrayと、行にあるセクションの数を確認してください...

于 2012-08-20T05:44:15.937 に答える