我在下面的错误,这是由该件的下面的代码引起的:iPhone应用崩溃,因为“索引0超出界限为空数组”
错误
Terminating app due to uncaught exception 'NSRangeException', reason: ' -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
newsDetailView *NieuwsDetailView = [[newsDetailView alloc] init];
[NewsDetailView setAlleItems:newsItems];
[NewsDetailView setCurrentItemId:indexPath.row];
NSLog(@"%@", newsItems);
[self.navigationController pushViewController:NewsDetailView animated:YES]; // This line gives the error
[NewsDetailView release];
}
我有从充满新闻项目的的UITableView在线XML提要。我正在使用XMLTouch解析。当我点击某个项目以查看详细视图时,它会崩溃。
我google了这个问题,但我确实计数了- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {}
方法中的所有数组项(返回[newsItems count];
)。
另外我正在记录完整的newsItems数组,正好在崩溃之上。我可以告诉你,这不是空的。
因此,如果它不是行数,而不是一个空数组。它是什么?
一个奇怪的(?)事情: 我总是得到这个错误:“索引超越空阵列的界限”。即使我点击了第三行或第四行,这会给我一个错误,如“索引超出空数组边界”或其他。上详细视图的.m **
- (void)viewDidLoad {
[super viewDidLoad];
[self setTitle:@"News"];
}
- (void)loadView {
[super loadView];
NSString *titel;
NSString *bericht;
NSString *datum;
NSString *foto;
titel = [[newsItems objectAtIndex: currentItemId] objectForKey: @"titel"];
bericht = [[newsItems objectAtIndex: currentItemId] objectForKey: @"bericht"];
datum = [[newsItems objectAtIndex: currentItemId] objectForKey: @"datum"];
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2) {
foto = [[newsItems objectAtIndex: currentItemId] objectForKey: @"fotoRetina"];
} else {
foto = [[newsItems objectAtIndex: currentItemId] objectForKey: @"foto"];
}
// Code I've ommited to keep things clear.
// I created a scrollView and put stuff in it, here. If anybody is interested in seeing this I can update this.
}
// No viewWillAppear, setAlleItems or setCurrentItemId methods
你确定是不是代码?我看不到'[self.navigationController NewsDetailView动画:YES];'应该甚至能够编译。另一个提示:请使用小写第一个字母作为变量,大写首字母作为类别。这使得其他人更容易阅读你的代码。 – tonklon
我想问题是NewsDetailView的实现。此外,您标记为有问题的行不是有效的objective-c。 – djromero
@madmw什么是无效的? – Sven