0

編集 - 解決済み:問題は、テーブルビューに入れるすべてのオブジェクトを含む文字列を作成したときでした。「?」を使用しました。各オブジェクトとその文字を含むオブジェクトの 1 つを分離するため、配列が壊れました。 [myString appendFormat:@"%@$%@$%@$%@?",
[currentCampeggioDict valueForKey:@"id"],
[currentCampeggioDict valueForKey:@"nome"],
[...]

NS Array *elencoCampeggi=[myString componentsSeparatedByString:@"?"];

That's it.


I have this problem: my app receives from an URL a XML file with a list of objects. The tableview is populated by those objects. There are so many items, so I have to call several times the url, changing a parameter in it for example :

http://www.myurl.com?method=xxx&PAGE=1

http://www.myurl.com?method=xxx&PAGE=2

etc..

The last cell of the tableview contains the sentence "Click to load next 25 objects" and it changes the page number for the URL, delete itself and reload the url, adding the new objects to the tableview.

After 4 times everything works great, but when I press the cell for the fifth, an exception appears with the following text:

Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 17 beyond bounds [0 .. 16]'

Any help? Thanks.

EDIT: some code: this is the part where I delete the last cell

-(void) loadTestData { // fill moviesArray with test data

NSInteger lastRow = [CampeggiArray count]-1;
if (lastLoadedPage > 1){
    [CampeggiArray removeObjectAtIndex:lastRow];
    [self.tableView reloadData];
}

[...]

4

1 に答える 1

0

問題は、テーブルビュー デリゲート メソッドで間違った行数を返しているためです。テーブルビューにリストするオブジェクトの総数を特定し、テーブルビューのデリゲート メソッドでカウントを返す必要があります。これはページ サイズではありません。

ヒットごとに、新しいオブジェクトを既存のデータソース (NSArray) に追加し続け、テーブルをリロードする必要があります。最初のヒット時にデータソースにオブジェクトはありません。

デリゲート メソッドで 15 ではなく 16 を返していると思いますので、例外でクラッシュします。デリゲート メソッドで値をハード コードしないでください。オブジェクトの総数 (セット全体) を取得し、その数を返します。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section                {
   return [totalObjects count];

}

于 2013-04-16T14:27:24.910 に答える