尝试删除单元格和节时,UITableView崩溃

ins*_*anj 4 iphone crash xcode uitableview

我一直在努力创建一个UITableView,其内容从NSUserDefaults对象加载.但是,每当我尝试删除一个单元格/节时,程序崩溃并吐出以下内容:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1030
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).
Run Code Online (Sandbox Code Playgroud)

无论我多少读过这个错误并尝试修复它,我都被困在这里过去几个小时.这是我的方法,当用户点击"删除"按钮时调用该方法:

 (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //[[[NSUserDefaults standardUserDefaults] objectForKey:@"rawArray"] removeObjectAtIndex:indexPath.row];
        //[[[NSUserDefaults standardUserDefaults] objectForKey:@"rawDates"] removeObjectAtIndex:indexPath.row];

        NSMutableArray *rawArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"rawArray"];
        NSMutableArray *rawDates = [[NSUserDefaults standardUserDefaults] objectForKey:@"rawDates"];

        [rawArray removeObjectAtIndex:indexPath.row];
        [rawDates removeObjectAtIndex:indexPath.row];

        [[NSUserDefaults standardUserDefaults] setObject:rawArray forKey:@"rawArray"];
        [[NSUserDefaults standardUserDefaults] setObject:rawDates forKey:@"rawDates"];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight];

    }    
}
Run Code Online (Sandbox Code Playgroud)

以下是用于确定行数和单元格数的方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [[[NSUserDefaults standardUserDefaults] arrayForKey:@"rawDates"] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return [[[NSUserDefaults standardUserDefaults] arrayForKey:@"rawDates"] objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

那里的最后一个方法返回一个,因为到目前为止,我计划每个部分只有一个单元格.感谢您抽出宝贵时间来看看这里,我真的希望您能帮助我!:/

Pau*_*l.s 15

查看行和节批量插入,删除和重新加载

基本上你需要调用这些方法

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation;
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
Run Code Online (Sandbox Code Playgroud)

之间

- (void)beginUpdates;
- (void)endUpdates;
Run Code Online (Sandbox Code Playgroud)

例如

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight];
[tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)

您的dataSource还需要在调用endUpdates时反映更改


就个人而言,我喜欢创建一个类别来给出一个基于块的包装器

  1. 阻止我忘记打电话 endUpdates
  2. 它允许缩进看起来正确/自动

分类 UITableView

@interface UITableView (PSAdditions)

- (void)ps_updates:(void (^)(void))updateBlock;

@end

@implementation UITableView (PSAdditions)

- (void)ps_updates:(void (^)(void))updateBlock;
{
    [self beginUpdates];
    if (updateBlock) { updateBlock(); }
    [self endUpdates];
}
Run Code Online (Sandbox Code Playgroud)

这看起来像这样

[tableView ps_updates:^{
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight];
}];
Run Code Online (Sandbox Code Playgroud)