2013-02-01 60 views
0

我有一個UITableView,它顯示用戶保存的pdf文件(來自Documents文件夾)的列表。從UITableView刪除行的問題

當我嘗試刪除表中的唯一行時,它工作正常。

但是,當行是一多,我嘗試去除第三排,例如,我得到..

*** 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,在這種情況下,我刪除了行,否則索引集,但沒有任何運氣。

這是我使用

- (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]; 
} 

代碼在哪裏[自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

您不能從tableview中刪除單元格,而且您必須修改該數組以取決於您要創建的tableviewcells – Exploring

回答

0

哇,完美的時機。 不久後,我寫了這個答案,我做了一些調整的方法[self refreshTable],移動[自fillArray]方法viewDidLoad和[自refreshTable]到viewDidAppear 所以現在我有

-(void)viewDidLoad{ 
    [self fillArray]; 
} 

-(void)viewDidAppear:(BOOL)animated{ 
    [self refreshTable]; 

}

行刪除完美。

感謝您的時間,但。

3

這裏的問題是,您應該在調用deleteRowsAtIndexPaths之前從pdfArray中移除對象。所以,你打電話之前deleteRowsAtIndexPaths添加此::

[self.pdfArray removeObjectAtIndex:indexPath.row]; 

,因爲deleteRowsAtIndexPaths刪除從的tableView行(S)和完全不修改模型。你必須自己做。單元格被刪除,但如果您在從模型中刪除該行之前調用reloadData。細胞將重新出現。所以它期望你更新你的模型。

+0

我將該調用放在最後,因爲我仍然需要它從Documents文件夾中刪除PDF ,那麼我可以從數組中移除該對象。 – Phillip

+0

因此,只需要移動deleteRowsAtIndexPaths:removeObjectAtIndexPath之後: –