尝试从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]);
}
}
答案 0 :(得分:3)
你必须分三个阶段对表格进行修改。
1)处理你的模特
[documentURLs removeObjectAtIndex:row];
2)处理你的桌子动画
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
3)处理您的文件更新(可能之前可能会因为您可能在删除文件时出错)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSURL *urlToDelete = nil;
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSInteger row = [indexPath row];
urlToDelete = [documentURLs objectAtIndex:row];
[documentURLs removeObjectAtIndex:row]; // you need to update your model
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// do stuff to update the file here
}
}
或
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSURL *urlToDelete = nil;
if(editingStyle == UITableViewCellEditingStyleDelete) {
NSInteger row = [indexPath row];
urlToDelete = [documentURLs objectAtIndex:row];
// do stuff to update the file here...
if(success)
{
[documentURLs removeObjectAtIndex:row]; // you need to update your model
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
}
请注意。如果从模型中移除项目并且模型计数为eqaul为0,则必须注意。在这种情况下,您必须删除整个部分。
希望它有所帮助。
P.S。检查代码,因为我写的没有XCode
答案 1 :(得分:0)
你需要在开始和结束更新中包含删除,例如
[self.wallsTable beginUpdates]; //<<<<<<<<<<<<<<<<<<<<<<<<<
NSArray *paths = [NSArray arrayWithObject: deleteView.cellPath];
[self.wallsTable deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
[[self.wallEntries objectAtIndex:[deleteView.cellPath section]] removeObjectAtIndex:[deleteView.cellPath row]];
[self.wallsTable endUpdates]; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
所以你需要从桌子上移除这些
中的细胞答案 2 :(得分:0)
步骤3应该在步骤2的if块内 - 如果editStyle被删除则只执行删除。 否则,您将从表中删除行而不调整其他数据结构。
//2. If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSInteger row = [indexPath row];
urlToDelete = [documentURLs objectAtIndex:row];
[documentURLs removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}