iOS deleteRowsAtIndexPaths报错

来源:互联网 发布:js string length 编辑:程序博客网 时间:2024/06/05 00:29

问题1

今天遇到删除一行的cell报错。我的代码

 MyTaskRecordModel *model=sender.object;

        NSInteger tem=[_dataArrayindexOfObject:model];

        NSLog(@"%ld",(long)tem);

        NSIndexPath *indepath=[NSIndexPathindexPathForRow:teminSection:0];

        [_dataArrayremoveObject:

indepath

];

        [self.tableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObjects:indepath,nil]withRowAnimation:UITableViewRowAnimationTop];


看来看去 么有错啊。先是删除数据,然后再从ui页面中删除。百思不得。

然后没有办法了,看报错的日志把


 *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.30.14/UITableView.m:1720

** 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 (40) must be equal to the number of rows contained in that section before the update (40), 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).'


* *终止应用程序由于异常“NSInternalInconsistencyException”,原因是:“无效的更新:无效0节的行数。包含在一个现有的行数部分更新后(40)必须等于之前部分中包含更新的行数(40),加上或者减去从部分插入或删除的行数(1 0插入、删除)和正负的行数的进入或部分(0搬进来,0搬出去)

0 进 0 出? ,不能的啊,我已经删除 

[_dataArray removeObject:

indepath

];。 

仔细一看, NSIndexPath *indepath ,不是数据,我操。

应该是         [_dataArrayremoveObject:model];


小细节 浪费了很多时间,改完! 一切ok!



问题2

attempt to delete row 1 from section 0 which only contains 1 rows before the update'


我用的block  上错误代码

       dispatch_async(dispatch_get_main_queue(), ^{                        [self hideHud];            if (!error) {                BOOL success=[[dic2 objectForKey:@"success"]boolValue];                if (success ) {                                        NSInteger d=[_dataArray indexOfObject:dic];                    [_dataArray removeObject:dic];                    [_tableview deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];                                    }else{                    [StatusBarConfig showMsg:@"error 错误代码1001"];                }            }else{                [StatusBarConfig showMsg:@"error 错误代码1002"];                            }                    });

由于block 的特性。对 indexPath 创建了一个备份,所以删除的时候,找不到对应地方。

解决办法,不用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
中的 indexPath自己获得 indexPath

                    NSInteger d=[_dataArray indexOfObject:dic];                    [_dataArray removeObject:dic];                    NSIndexPath *path=[NSIndexPath indexPathForItem:d inSection:0];                    [_tableview deleteRowsAtIndexPaths:[NSArray arrayWithObjects:path, nil] withRowAnimation:UITableViewRowAnimationTop];    


问题解决




0 0