编程新手为任何愚蠢的问题道歉。代码中没有错误,但它来自我在模拟器上测试我的应用程序。我刚写了一个代码,每次你点击一个任务,它会改变颜色并将其移动到下面,我很兴奋,我一直点击它们,我点击一个已经标记完成并改变颜色,我想我的代码我没有为此做好准备,所以无论我写的是什么新代码我都可以永远不会让应用程序再次运行而不会崩溃。见下面的代码
- (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];
}
答案 0 :(得分:2)
您正试图在其中一个阵列上访问某些不存在的数据。
[__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];
}
答案 1 :(得分:0)
使用[数组计数]这种方法检查&#34; completedTasks&#34;是空的,你不能从空数组中获取数据。