【发布时间】:2012-01-21 15:12:53
【问题描述】:
尝试从 UITableView 中删除一行时出现此错误。如果我删除 tableview 中的最后一行,则不会出错并且一切正常,但任何其他行都会引发异常。有人可以告诉我我在这里做错了什么吗?任何帮助将不胜感激!
错误:
'无效更新:第 1 节中的行数无效。更新 (2) 后现有节中包含的行数必须等于更新前该节中包含的行数 (2),加上或减去从该节插入或删除的行数(0 插入,1 删除),加上或减去移入或移出该节的行数(0 移入,0 移出)。'
代码
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//1. clear existing URL's
NSURL *urlToDelete = nil;
//2. If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSInteger row = [indexPath row];
urlToDelete = [documentURLs objectAtIndex:row];
//[documentURLs removeObjectAtIndex:row];
}
//3. update the tableview on the fly without reloading the data
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
//4. get the location of the files
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathToDocumentsDirectory = [paths objectAtIndex:0];
//5.setup file manager
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL fileExists = [fileManager fileExistsAtPath:pathToDocumentsDirectory];
NSLog(@"Path to file: %@", pathToDocumentsDirectory);
NSLog(@"File exists: %d", fileExists);
NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:pathToDocumentsDirectory]);
//6. remove if matches
if (urlToDelete) {
BOOL success = [fileManager removeItemAtURL:urlToDelete error:&error];
if (!success) NSLog(@"Error: %@", [error localizedDescription]);
}
}
【问题讨论】:
-
尝试检查一下 - 可能是您的问题:stackoverflow.com/questions/5043729/…
-
//3. update the tableview on the fly without reloading the data [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];仅在成功为真时才尝试放入第六步。您的模型和表格视图中的行之间存在不一致
标签: objective-c ios cocoa-touch ipad