Objective c UITableView提交编辑错误
我有一个UITableView,其中填充了从NSUserDefaults读取的可变数组。我希望用户能够删除当前的一些项目。以下是特定于编辑方法的代码,以下是整个文件: 当我单击“编辑”并删除某个项目时,应用程序崩溃,我返回: 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[\uu nsFarray removeObjectAtIndex:]:正在将方法发送到不可变对象” 我的具体问题是,我在这里做错了什么Objective c UITableView提交编辑错误,objective-c,ios,cocoa-touch,Objective C,Ios,Cocoa Touch,我有一个UITableView,其中填充了从NSUserDefaults读取的可变数组。我希望用户能够删除当前的一些项目。以下是特定于编辑方法的代码,以下是整个文件: 当我单击“编辑”并删除某个项目时,应用程序崩溃,我返回: 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[\uu nsFarray removeObjectAtIndex:]:正在将方法发送到不可变对象” 我的具体问题是,我在这里做错了什么 // Override
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// edit list of names
if ([listOfNames count] >= 1) {
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[listOfNames removeObjectAtIndex:[indexPath row]];
// write updated listofnames to nsuserdefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[[NSUserDefaults standardUserDefaults] setObject:listOfNames forKey:@"My Key"];
[defaults synchronize];
if ([listOfNames count] == 0) {
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[tableView endUpdates];
}
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
您的
listOfNames
实例变量是不可变的,因此无法从中删除对象
更改:
listOfNames = [[defaults objectForKey:@"My Key"] copy];
到
在您的
-视图中将出现:
和viewDidLoad
方法。Gah已将错误版本的文件复制/粘贴回实时项目。我一直在说。。“我知道我改变了。”。。谢谢你,格雷弗。当它允许我时,将选择此为正确。
listOfNames = [[defaults objectForKey:@"My Key"] mutableCopy];