iphone - uiTableView 在删除单元格时第二次崩溃

标签 iphone ios ipad uitableview

我是 iPhone 的新手,我试图从我的 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 (3) must be equal to the number 
of rows contained in that section before the update (3), 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).'

这是我的表格代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
Book_own *temp= (Book_own *)[self.books objectAtIndex:indexPath.row];

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source

    [books removeObjectAtIndex:indexPath.row];

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    isDeleted = @"true";
    deletedBook_id = temp.bo_id;

    [self viewDidLoad];

}

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
}  

}


 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.

NSDictionary *dictionary = [listOfItems objectAtIndex:section];
NSArray *array = [dictionary objectForKey:@"myBooks"];
return [array count];
}

在 ViewDidLoad 中我写了下面的代码:

NSDictionary *mbooks = [NSDictionary dictionaryWithObject:books forKey:@"myBooks"];

NSDictionary *mgroups = [NSDictionary dictionaryWithObject:filteredParts forKey:@"myBooks"];
listOfItems = [[NSMutableArray alloc] init];

[listOfItems addObject:mbooks];
[listOfItems addObject:mgroups];

谁能告诉我如何解决这个问题? 提前致谢。

最佳答案

发生的情况是,当您允许多次删除时,您要么没有删除该项目,要么只删除了一个项目。您可以通过检查它是否真的被删除来调整,如果没有,则重新加载数据:

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        BOOL success = [self removeFile:indexPath];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        if (!success) [tableView reloadData];
    }
}

此方法然后删除数据源项(它来 self 自己的项目,因此应调整名称:

-(BOOL) removeFile:(NSIndexPath *)indexPath{
    // Removes from the datasource and the filesystem
    NSURL *fileURL = [self.dataSource objectAtIndex:indexPath.row];
    NSError *error;
    BOOL success = [[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error];
    if (success) {
        [self.dataSource removeObjectAtIndex:indexPath.row];
    } else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[error localizedDescription] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
        [alert release];
        [self.dataSource removeObjectAtIndex:indexPath.row];
    }
    return success;
}

关于iphone - uiTableView 在删除单元格时第二次崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12164466/

相关文章:

iphone - 禁用以肖像 AVFoundation 以外的任何方向拍摄图像

ios - 更改不相关 View 的 z 顺序

iphone - 在 iOS 5 中自定义导航栏

iphone - 选择模拟器后,如何在 XCode 中自动启用#define?

iphone - 向导航栏添加标题

iphone - 如何使用不同高度的 UISCrollView 分页?

iphone - 当我将触摸事件传递给 super View 时,MyScrollView 不响应触摸

objective-c - 如何在 iOS 应用程序中使用 Berkeley DB?

ios - 如何在 ios 中获取 YTPlayer 完成按钮状态?

ios - Chartboost 插页式 : "not in the window hierarchy!" warning