这是打印到控制台的错误:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '***
-[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
(0x27f0012 0x1a51e7e 0x27a5b44 0xde6d 0xe2f8fb 0xe2f9cf 0xe181bb 0xe28b4b 0xdc52dd 0x1a656b0 0x97efc0 0x97333c 0x973150 0x8f10bc 0x8f2227 0x99c333 0x99c75f 0x27af376 0x27aee06 0x2796a82 0x2795f44 0x2795e1b 0x22677e3 0x2267668 0xd74ffc 0x258d 0x24b5 0x1)
libc++abi.dylib: terminate called throwing an exception
该程序恰好在以下位置中断:
NSString *cellValue = [models objectAtIndex:indexPath.row];
从cellForRowAtIndexPath:方法中
以下是所有的 tableview 方法:
//---set the title for each section---
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//countyIndex returns the number of counties per state
//in the debugger I checked and the countyIndex is returning the correct number
//of counties!
return [countyIndex objectAtIndex:section];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [countyIndex count];
}
//---set the number of rows in each section---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//---get the county in section---
NSString *county = [countyIndex objectAtIndex:section];
//---get all models from the county---
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"countyName == %@", county];
NSArray *mods = [modelArray filteredArrayUsingPredicate:predicate];
//---return the number of models from the county---
return [mods count];
}
//models appears to be counted as the number of objects per section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// now configure the cell
//---get the county in the current section---
NSString *county = [countyIndex objectAtIndex:[indexPath section]];
//---get all models from the county---
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"countyName == %@", county];
NSArray *mods = [modelArray filteredArrayUsingPredicate:predicate];
NSMutableArray *newModelArray = [[NSMutableArray alloc] init];
for (NSManagedObject *obj in mods) {
[newModelArray addObject:[NSString stringWithFormat:@"%@",[obj valueForKey: @"model"]]];
}
//get rid of duplicates
NSArray *objectsWithDuplicates = newModelArray;
NSSet *duplicatesRemover = [NSSet setWithArray:objectsWithDuplicates];
models = [duplicatesRemover allObjects];
models = [models sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
UITableViewCell *bgView = [[UITableViewCell alloc] initWithFrame:CGRectZero];
//cell.contentView.backgroundColor = [UIColor whiteColor];
bgView.backgroundColor = [UIColor whiteColor];
cell.backgroundView = bgView;
cell.layer.borderColor = [[UIColor lightGrayColor] CGColor];
cell.layer.borderWidth = .50;
if ([models count]>0) {
//---extract the relevant model from the model object---
NSString *cellValue = [models objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
[cell setAccessibilityTraits:UIAccessibilityTraitButton];
}
return cell;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//index path to row that's selected
NSIndexPath *myIndexPath = [self.sTable indexPathForSelectedRow];
UITableViewCell *cell = [self.sTable cellForRowAtIndexPath:myIndexPath];
labelText = cell.textLabel.text;
selectedModel = cell.textLabel.text;
}
从控制台执行一些命令后:
po [型号数]
po (int)[indexPath 行]
我看到当 [models count] 等于 2 并且 [indexPath row] 也等于 2 时它会中断,这当然会使它超出范围,因为 2 实际上是 3,因为表索引从 0 开始。
问题是,为什么会发生这种情况?在切换表值后,我已经重新加载了表数据,我检查并计算了每个部分的所有行数,它们看起来是正确的。那么为什么还会发生这种情况呢?奇怪的是,当我在表格中向下滚动时,重新调用 cellForRowAtIndexPath:(NSIndexPath *)indexPath 之后会发生这种中断。
这发生在 Xcode 4.6.1 中的所有模拟器 ios 5.0 - ios 6.1
**编辑:
我应该补充一点,这只发生在加利福尼亚州和威斯康星州。我认为这将有助于缩小范围,但我已经检查了数据库,并且县和模型的数量与调试器中返回的内容相匹配。