3

@ "Comments"配列が0で、他の2つが1以上の場合、次のエラーが発生します。

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'

これはself.itemsアレイにデータを入力しています。

self.notification一部の(この場合はコメント)配列がnullであり、このタイプのエラーをスローせずにコードを実行できるという事実をどのように説明できますか?

NSDictionary *commentsDict = [NSDictionary dictionaryWithObject:self.notification.Comments forKey:@"Comments"];
NSDictionary *likesDict = [NSDictionary dictionaryWithObject:self.notification.Likes forKey:@"Likes"];
NSDictionary *friendsDict = [NSDictionary dictionaryWithObject:self.notification.Likes forKey:@"Friends"];

self.items = [NSMutableArray array];

[self.items addObject:commentsDict];
[self.items addObject:likesDict];
[self.items addObject:friendsDict];

これは失敗するコードです:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dictionary = [self.items objectAtIndex:section];
    NSArray *array = nil;

    if (section == 0) {
        array = [dictionary objectForKey:@"Comments"]; // 0 count
    } else if (section == 1) {
        array = [dictionary objectForKey:@"Likes"]; // 1 count or greater
    } else {
        array = [dictionary objectForKey:@"Friends"]; // 1 count or greater
    }

    return [array count];
}
4

5 に答える 5

2

コードの前にを置くNSLog(@"Array: %@", self.items);と、配列が実際に空であることがわかります。

于 2011-03-02T09:19:21.050 に答える
1

そこにないインデックスでオブジェクトを取得しようとしています。おそらく、self.items配列には各セクションインデックスのオブジェクトがありません。エラーメッセージから、self.items実際には空(オブジェクトがゼロ)のように見えます。

于 2011-03-02T09:17:56.530 に答える
1

定義されているセクションが多すぎる(self.itemsのアイテム数より多い)可能性があります。

次に、これは失敗します:

[self.items objectAtIndex:section]
于 2011-03-02T09:19:04.700 に答える
1

self.itemsこれを解決するには、設定方法と実装方法を確認する必要があります- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

self.items配列を早期に解放するか、セクションの数を返す方法はに依存しませんself.items

于 2011-03-02T20:48:22.563 に答える
0

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;セクションヘッダーを変更するときに他の2つではなく、Comments配列のみを参照していたため、これは私の問題でした。

于 2011-03-06T07:34:43.893 に答える