Objective c TableView中具有[indexPath行]节的NSRangeException超出范围
以下是打印到控制台的错误:Objective c TableView中具有[indexPath行]节的NSRangeException超出范围,objective-c,uitableview,nsindexpath,nsrangeexception,Objective C,Uitableview,Nsindexpath,Nsrangeexception,以下是打印到控制台的错误: *** 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 0
*** 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(int)[indexPath行]
我看到当[models count]等于2,[indexPath行]也等于2时,它就被打破了,这当然会使它超出范围,因为2实际上是3,因为表索引从0开始
问题是,为什么会发生这种情况?在切换表值之后,我已经重新加载了tabledata,我检查并计算了每个部分的所有行数,它们看起来是正确的。那么为什么会发生这种情况呢?奇怪的是,当我在表中向下滚动时,在重新调用CellForRowatineXpath:(NSIndexPath*)indexPath之后,会发生这种中断
这发生在Xcode 4.6.1中的所有模拟器ios 5.0-ios 6.1中
**编辑:
我应该补充一点,这只发生在加利福尼亚州和威斯康星州。
我原以为这会有助于缩小范围,但我已检查了数据库,调试器中返回的县数和模型数是匹配的。这是因为在
cellforrowatinexpath
中删除了重复项。但是在numberOfRows
中不能这样做。因此,cellForRow
中的模型数组比numberOfRows
中导出的计数要短
这是一个典型的例子,说明为什么干燥是如此重要的规则:不要重复你自己。用两种不同的方法做同一件事,不仅浪费了代码,使代码难以维护,而且在这两个地方做同样的事情可能会有不同的风险。您应该考虑为特定区段(县)导出实际模型的代码,以便只有一个方法供其他两个方法参考。(更好的做法可能是准备一个更好的模型:行和数组索引之间始终只有简单的一对一对应关系。)这是因为
模型在返回节中的行数之前发生了变异,或者返回了错误的行数。。。也许是因为你现在有多个部分?你不包括那些代码。我有一个带有分区的表视图。正在计数的模型数量似乎是每个部分的数量。您没有包含足够的代码来查看可能发生的情况。。。要么你告诉框架你拥有的模型比你实际拥有的要多,要么你正在失去它们。。。可能是第一个,而不是返回给定节的行数的部分…我正在检查这是否是导致问题的原因,我会让你知道它是如何工作的。令人惊讶的是,没有。我很震惊,因为我添加了移除的副本(顺便说一句,它应该有)因此,这应该是导致错误的原因…但即使在修复之后,它仍然会给我一个nsrangexception!我可能不得不从悬崖上跳下去…:(但说真的,事情会是这样的。做我说的另一件事:重构,这样模型在两个地方都能以完全相同的方式导出。更妙的是,正如我说的,使用相同的模型:提前准备模型,这样就不会有任何差异。这种必须用两种方法计算模型的业务只会要求出错(显然,我明白了)唯一的问题是,在这个视图中,我有一个选择器控制器,它保存所有的状态,每次状态改变时,都会列出所有的县及其机器型号。因此,值会改变,这就需要重新计算它们。