OStack程序员社区-中国程序员成长平台

标题: iphone - 从 UITableView 插入和删除行时如何正确更新数据源? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:55
标题: iphone - 从 UITableView 插入和删除行时如何正确更新数据源?

我正在尝试从 UITableView 中删除一些行,这是我的代码...

    [self.responsesTableView beginUpdates];
    [self.responsesTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.processingIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self. responsesTableView endUpdates];
    noOfRsponses --;
    if (self.segFrndEveryone.selectedSegmentIndex == 0) {
        [self.arrFriendComments removeObjectAtIndex:self.processingIndexPath.row];
    }else {
        [self.arrAllComments removeObjectAtIndex:self.processingIndexPath.row];
    }

其中两个数组之一是我的数据源,取决于所选的段。 我收到以下错误,不知道如何处理。

The number of rows contained in an existing section after the update (9) must be equal to the number of rows contained in that section before the update (9), 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).'



Best Answer-推荐答案


您应该在手动更改 UITableView 之前更新您的数据源。

// update the data source for the table change
noOfRsponses--;
NSMutableArray *targetArray = (self.segFrndEveryone.selectedSegmentIndex == 0) ? self.arrFriendComments : self.arrAllComments;
[targetArray removeObjectAtIndex:self.processingIndexPath.row];

// update the table view itself
[[self responsesTableView] beginUpdates];
[[self responsesTableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.processingIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[[self responsesTableView] endUpdates];

关于iphone - 从 UITableView 插入和删除行时如何正确更新数据源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13759553/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4