我有一个UITableView,显示用户保存的pdf文件列表(来自Documents文件夹)。
当我尝试删除表中唯一的行时,它可以正常工作。
但是当行多于一行时,我尝试删除第三行,例如,我得到..
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
现在,我试图检查我的数组计数是否为> 0,在这种情况下我删除了行,否则删除了indexSet,但没有任何运气。
这是我正在使用的代码
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory
stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",[self.pdfArray objectAtIndex:indexPath.row]]];
if ([fileMgr removeItemAtPath:filePath error:&error] != YES)
{ UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:NSLocalizedString(@"UnableToDeleteFile", @"Unable to delete file: %@"),[error localizedDescription]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] autorelease];
[alert show];
}
[self.pdfArray removeObjectAtIndex:indexPath.row];
[self refreshTable];
[tableView endUpdates];
}
[self.tableView reloadData];
}
其中[self refreshTable]显示带有标签的视图,表明没有保存PDF:
- (void) refreshTable {
[self fillArray];
[self.tableView reloadData];
if (self.pdfArray.count == 0)
{
[UIView animateWithDuration:1.0f animations:^{
noPDF.view.alpha = 1;
}];
self.navigationItem.rightBarButtonItem = nil;
self.tableView.userInteractionEnabled = NO;
}
else
{
noPDF.view.alpha = 0;
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.tableView.userInteractionEnabled = YES;
}
}
现在,我实际上无法得到如何解决这个问题,所以,任何帮助都非常感谢!
答案 0 :(得分:3)
这里的问题是你应该在调用deleteRowsAtIndexPaths之前从pdfArray中删除对象:。所以添加这个::
[self.pdfArray removeObjectAtIndex:indexPath.row];
在调用deleteRowsAtIndexPaths之前,因为deleteRowsAtIndexPaths从tableView中删除一行,并且根本不修改您的模型。你必须自己做。单元格将被删除,但如果在从模型中删除行之前调用reloadData。细胞将重新出现。所以它希望你更新你的模型。
答案 1 :(得分:0)
[self refreshTable]
进行了一些调整,将[self fillArray]方法移至viewDidLoad
,将[self refreshTable]移至viewDidAppear
所以现在我有了
-(void)viewDidLoad{
[self fillArray];
}
-(void)viewDidAppear:(BOOL)animated{
[self refreshTable];
}
行删除工作完美。
谢谢你的时间。