NSRangeException - NSArrayM objectAtIndex

时间:2013-04-16 14:14:46

标签: ios objective-c nsarray tableview

编辑 - 已解决:问题是我创建了包含要放入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];
}

tableview

1 个答案:

答案 0 :(得分:0)

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

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

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

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

}