【发布时间】:2015-04-13 23:07:43
【问题描述】:
我的桌子一直在崩溃。 “索引 1 超出范围”。 我返回该部分中行数的数组计数,但是一旦滚动到第二行(索引 1),应用程序就会崩溃。
我有两个部分。一个具有固定的行数,第二个部分是根据数组中的项目数动态创建的。
有什么想法吗?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2; //two seperate areas
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 16;
} else {
NSLog(@"Section: %ld, NumberOfRows: %lu", (long)section, (unsigned long)[self.arrImages count]);
return [self.arrImages count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
switch (section) {
case 0:
cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
break;
case 1:
cell = [tableView dequeueReusableCellWithIdentifier:@"cellPhoto"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellPhoto"];
}
NSLog(@"Row: %lu\n%@", (unsigned long)row, [[self.arrImages objectAtIndex:row] objectForKey:@"imageThumbnailURL"]);
cell.imageView.image = [self imageFromURL:[[self.arrImages objectAtIndex:row] objectForKey:@"imageThumbnailURL"] fromCache:YES];
NSLog(@"completed");
break;
}
return cell;
}
#pragma mark UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
[self doneData];
switch (section) {
case 1:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self performSegueWithIdentifier:@"segue_showphoto" sender:self];
break;
}
}
这是我的崩溃日志
2015-02-12 19:32:51.817 Tops[17236:5681531] Row: 0
https://somethinghere.jpeg
2015-02-12 19:32:51.825 Tops[17236:5681531] completed
2015-02-12 19:32:51.855 Tops[17236:5681531] Row: 1
https://somthinghere.jpeg
2015-02-12 19:32:51.856 Tops[17236:5681531] completed
2015-02-12 19:32:51.862 Tops[17236:5681531] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
【问题讨论】:
-
我认为你应该设置一个exception breakpoint 并查看你正在访问的数组越界,它的内容是什么,以及为什么你还没有填充它。
-
设置异常断点。我认为您的应用不会在此代码中崩溃。
-
当我听到这样的问题时,我直接去
numberOfRowsInSection并确认它返回了某个数组的计数,然后我查看cellForRowAtIndexPath并确保代码取消引用相同的数组以相同的方式。您的代码在该测试中的 50% 好(顺便说一句,高于平均水平 :-))。cellForRowForIndexPath使用第 0 节的超级实现,那么numberOfRowsInSection也应该如此。我会给出这个答案,但我同意其他评论者的观点:我们不确定问题出在哪里。 -
让我发疯的是:如果我为 numberOfRowsInSection 返回 1,那么一切都很好。我会打破一切,看看我想出了什么。
-
了解如何获取异常堆栈跟踪。
标签: ios objective-c uitableview nsarray