编程新手,所以对任何愚蠢的问题表示歉意。 代码中不存在错误,但它来自我在模拟器上测试我的应用程序。 我刚刚编写了一个代码,每次单击任务时,它都会更改颜色并将其移动到下方,我很兴奋,我一直单击它们,我单击了一个已经标记为已完成并且已更改颜色的代码,我想我的代码没有为此做好准备,所以无论我写什么新代码,我都无法让应用程序再次运行而不会崩溃。 请参阅下面的代码
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[[self arrayForSection:indexPath.section]removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
// Tapped an uncompleted Task. Must complete it!
if (indexPath.section == 0) {
NSString *task = self.tasks[indexPath.row];
[self.tasks removeObjectAtIndex:indexPath.row];
[self.completedTasks insertObject:task atIndex:0];
[tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationTop];
}
// Tapped a completed Task. Time to make it an uncompleted task
else {
NSString *task = self.completedTasks[indexPath.row];
[self.completedTasks removeObjectAtIndex:indexPath.row];
[self.tasks insertObject:task atIndex:0];
[tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationTop];
}
[tableView endUpdates];
[self save];
}
您正在尝试访问其中一个数组中一些不存在的数据。
[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]
表示您的数组有 0 个对象,并且您正在尝试访问索引 1 处的对象(没有人)。这是错误的原因。
在从任何数组中获取或删除项目之前,您应该使用防御性编程。像这样:
if ([array count] < index)
{
[array removeObjectAtIndex: index];
NSString *string1 = [array objectAtIndex: index];
NSString *string2 = array[index];
}
使用[数组计数]此方法检查"已完成任务"是否为空,无法从空数组中获取数据。