Objective c 将一半以上的单元格移动到另一个分区时,Tableview崩溃
基本上,我正在尝试编写一个应用程序,它有一个包含两个部分的tableview,一个包含未完成的任务(单元格),另一个包含已完成的任务。每个单元格都有一个复选框UIButton子类。问题是,当我签出超过一半的项目时,它会因NSRangeException而崩溃。我非常感谢您对这个问题的任何建议 错误:*由于未捕获的异常“NSRangeException”而终止应用程序,原因:'*-[\uu NSArrayM objectAtIndex:]:索引9超出边界[0..8]' CheckMarkTapped方法(调用以切换项目): CELLFORROWATINDEXPATH方法:Objective c 将一半以上的单元格移动到另一个分区时,Tableview崩溃,objective-c,uitableview,Objective C,Uitableview,基本上,我正在尝试编写一个应用程序,它有一个包含两个部分的tableview,一个包含未完成的任务(单元格),另一个包含已完成的任务。每个单元格都有一个复选框UIButton子类。问题是,当我签出超过一半的项目时,它会因NSRangeException而崩溃。我非常感谢您对这个问题的任何建议 错误:*由于未捕获的异常“NSRangeException”而终止应用程序,原因:'*-[\uu NSArrayM objectAtIndex:]:索引9超出边界[0..8]' CheckMarkTappe
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
if (indexPath.section == 1) {
CellIdentifier = @"Cell1";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.showsReorderControl = YES;
}
for (UIControl *checkbox in [cell subviews]) {
if (checkbox.tag == 9001) {
[checkbox removeFromSuperview];
}
}
if (indexPath.section == 0) {
UIButton *checkbox = [[UIButton alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
[checkbox addTarget:self action:@selector(checkMarkTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell setIndentationWidth:45];
[cell setIndentationLevel:1];
[checkbox setTag:9001];
[checkbox setImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
[checkbox setImage:[UIImage imageNamed:@"checked"] forState:UIControlStateSelected];
[cell addSubview:checkbox];
cell.textLabel.backgroundColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor whiteColor];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:20];
switch (indexPath.section) {
case 0:
cell.textLabel.text = [[uncompletedItems objectAtIndex:indexPath.row]objectForKey:@"Name"];
break;
case 1:
cell.textLabel.text = [[completedItems objectAtIndex:indexPath.row]objectForKey:@"Name"];
[checkbox setSelected:YES];
break;
default:
break;
}
[cell addSubview:checkbox];
} else if (indexPath.section == 1) {
UIButton *checkbox = [[UIButton alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
[checkbox addTarget:self action:@selector(checkMarkTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell setIndentationWidth:45];
[cell setIndentationLevel:1];
[checkbox setTag:9001];
cell.textLabel.backgroundColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor whiteColor];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:20];
cell.textLabel.text = [[uncompletedItems objectAtIndex:indexPath.row]objectForKey:@"Name"];
[checkbox setImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
[checkbox setImage:[UIImage imageNamed:@"checked"] forState:UIControlStateSelected];
[cell addSubview:checkbox];
switch (indexPath.section) {
case 0:
cell.textLabel.text = [[uncompletedItems objectAtIndex:indexPath.row]objectForKey:@"Name"];
break;
case 1:
cell.textLabel.text = [[completedItems objectAtIndex:indexPath.row]objectForKey:@"Name"];
[checkbox setSelected:YES];
break;
default:
break;
}
[cell addSubview:checkbox];
}
return cell;
}
这是坠机事件的NSLog。如您所见,当数组具有相等数量的项时会发生这种情况:
2012-12-26 12:54:44.583 Checklist R4[1128:c07] Destination Row: 0, UnCompleted Count: 19, Completed Count: 1
2012-12-26 12:54:45.034 Checklist R4[1128:c07] Destination Row: 1, UnCompleted Count: 18, Completed Count: 2
2012-12-26 12:54:45.454 Checklist R4[1128:c07] Destination Row: 2, UnCompleted Count: 17, Completed Count: 3
2012-12-26 12:54:45.873 Checklist R4[1128:c07] Destination Row: 3, UnCompleted Count: 16, Completed Count: 4
2012-12-26 12:54:46.294 Checklist R4[1128:c07] Destination Row: 4, UnCompleted Count: 15, Completed Count: 5
2012-12-26 12:54:47.072 Checklist R4[1128:c07] Destination Row: 5, UnCompleted Count: 14, Completed Count: 6
2012-12-26 12:54:47.399 Checklist R4[1128:c07] Destination Row: 6, UnCompleted Count: 13, Completed Count: 7
2012-12-26 12:54:48.085 Checklist R4[1128:c07] Destination Row: 7, UnCompleted Count: 12, Completed Count: 8
2012-12-26 12:54:48.621 Checklist R4[1128:c07] Destination Row: 8, UnCompleted Count: 11, Completed Count: 9
2012-12-26 12:54:49.090 Checklist R4[1128:c07] Destination Row: 9, UnCompleted Count: 10, Completed Count: 10
2012-12-26 12:54:50.234 Checklist R4[1128:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 9 beyond bounds [0 .. 8]'
您的
completedItems
或uncompletedItems
数组,其中一个只有8个项目,但您尝试访问的项目不止这些。检查numberOfRowsInSection
方法,给出它们的动态值,如
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int rowCount = 0;
if (section == 0) {
rowCount = [uncompletedItems count];
} else {
rowCount = [completedItems count];
}
return rowCount;
}
而且它不需要同时使用开关
和if
。请使用任何人检查您的部分
是否为0或1
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
if (indexPath.section == 1) {
CellIdentifier = @"Cell1";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.showsReorderControl = YES;
}
for (UIControl *checkbox in [cell subviews]) {
if (checkbox.tag == 9001) {
[checkbox removeFromSuperview];
}
}
UIButton *checkbox = [[UIButton alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
[checkbox addTarget:self action:@selector(checkMarkTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell setIndentationWidth:45];
[cell setIndentationLevel:1];
[checkbox setTag:9001];
[checkbox setImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
[checkbox setImage:[UIImage imageNamed:@"checked"] forState:UIControlStateSelected];
[cell addSubview:checkbox];
cell.textLabel.backgroundColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor whiteColor];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:20];
switch (indexPath.section) {
case 0:
cell.textLabel.text = [[uncompletedItems objectAtIndex:indexPath.row]objectForKey:@"Name"];
break;
case 1:
cell.textLabel.text = [[completedItems objectAtIndex:indexPath.row]objectForKey:@"Name"];
[checkbox setSelected:YES];
break;
default:
break;
}
[cell addSubview:checkbox];
return cell;
}
您的代码中可能有两个问题
返回到表视图的行数小于数组的行数
计数。
您的数组超出了表中的行数
查看。
您描述的
错误
清楚地表明您的数组有8个
索引,但您正在访问导致错误的9个
索引。两件事:首先显示或说明返回了多少行/节,其次说明在哪一行代码中获得异常(如果您不知道,请添加异常断点).我知道是什么原因导致了错误,但我无法理解它为什么访问不存在的第九个索引。另外,奇怪的是,只有当两个部分中的项相等时才会发生这种情况。好吧,一个快速的解决方案是,我们可以进行检查,只有当索引
小于数组计数
时,才在tableView中插入数据。它不起作用。它以前崩溃过。我编辑了这篇文章以包含它崩溃的点。我检查了cellForRowAtIndexPath方法,发现它没有问题。还有其他想法吗?通过设置来尝试。我找不到你的代码中有什么问题。因为在按钮操作中,有时添加,有时插入<代码>符号断点将使您知道代码崩溃的位置。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
if (indexPath.section == 1) {
CellIdentifier = @"Cell1";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.showsReorderControl = YES;
}
for (UIControl *checkbox in [cell subviews]) {
if (checkbox.tag == 9001) {
[checkbox removeFromSuperview];
}
}
UIButton *checkbox = [[UIButton alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
[checkbox addTarget:self action:@selector(checkMarkTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell setIndentationWidth:45];
[cell setIndentationLevel:1];
[checkbox setTag:9001];
[checkbox setImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
[checkbox setImage:[UIImage imageNamed:@"checked"] forState:UIControlStateSelected];
[cell addSubview:checkbox];
cell.textLabel.backgroundColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor whiteColor];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:20];
switch (indexPath.section) {
case 0:
cell.textLabel.text = [[uncompletedItems objectAtIndex:indexPath.row]objectForKey:@"Name"];
break;
case 1:
cell.textLabel.text = [[completedItems objectAtIndex:indexPath.row]objectForKey:@"Name"];
[checkbox setSelected:YES];
break;
default:
break;
}
[cell addSubview:checkbox];
return cell;
}