0

编辑 - 已解决:问题是当我创建包含所有对象的字符串以放入 tableview 时。我用了“?” 分离每个对象和其中一个包含该字符的对象,因此数组中断。 [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

问题是因为您在 tableview 委托方法中返回了错误的行数。您需要确定要在 tableview 中列出的对象的总数,并在 tableview 委托方法中返回计数。这不是您的页面大小。

对于每次命中,您需要继续将新对象附加到已经存在的数据源(NSArray)并重新加载表。在初始命中期间数据源中将没有对象。

我猜,你在委托方法中返回 16 而不是 15,所以你会遇到异常崩溃。不要在您的委托方法中硬编码值,只需获取对象的总数(整个集合)并返回它的计数。

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

}

于 2013-04-16T14:27:24.910 回答