NSInternalInconsistencyException(无效行数)

sud*_*-rf 15 iphone cocoa-touch objective-c uitableview

每当我在我的数据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 (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'

这是我正在编辑的东西:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        if ([myData count] >= 1) {
            [tableView beginUpdates];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [myData removeObjectAtIndex:[indexPath row]];

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *somepath = [documentsDirectory stringByAppendingPathComponent:@"something.plist"];
            [myData writeToFile:somepath atomically:YES];
            [table reloadData];
            if ([myData count] == 0) {
                [tableView endUpdates];
                [tableView reloadData];
            }
            else {
            [tableView endUpdates];
            }
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

而且这个:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if ([myData count] != 0) {
        return [myData count];
    }
    else {
        return 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

我返回1的原因是因为我正在制作一个单元格,上面写着"没有数据保存" cellForRowAtIndexPath.这就是我的意思:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }    
    if ([cityData count] != 0) {
        //normal setup removed for clarity
    }
    else {
        cell.textLabel.text = @"No saved data!";
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; 
    cell.textLabel.textAlignment = UITextAlignmentCenter;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.tag = 1;
    return cell;
    }
}
Run Code Online (Sandbox Code Playgroud)

那么,我在编辑代码中出错是为了得到这个错误?谢谢!

Cam*_*ert 29

如果删除表中的最后一行,则UITableView代码期望剩余0行.它会调用您的UITableViewDataSource方法来确定剩余的数量.由于您有一个"无数据"单元格,它返回1,而不是0.因此,当您删除表格中的最后一行时,请尝试调用-insertRowsAtIndexPaths:withRowAnimation:以插入"无数据"行.此外,您不应该-reloadData在此方法中的任何地方调用.-endUpdates将负责重新加载受影响的行.试试这个:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        if ([myData count] >= 1) {
            [tableView beginUpdates];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [myData removeObjectAtIndex:[indexPath row]];

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *somepath = [documentsDirectory stringByAppendingPathComponent:@"something.plist"];
            [myData writeToFile:somepath atomically:YES];

            if ([myData count] == 0) {
                [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
            [tableView endUpdates];
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

  • 如果[data count]> 0并且之后DO调用reloadData,你还可以做的只是调用tableView deleteRowsAtIndexPaths.这样,如果它将用于"无数据"单元格,则不会删除最后一个单元格,并且重新加载会使委托在表格视图中加载正确的信息. (3认同)
  • 伙计,你不知道我对此感激不尽! (2认同)