iphone - 从 UITableView 插入和删除行时如何正确更新数据源?

标签 iphone objective-c ios 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).'

最佳答案

您应该在手动更改 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/

相关文章:

android - 为什么Android和IOS11无法通过NFC通信

iphone - UIScrollView放大后渲染内容

ios - 将 rx.value 添加到我的 CustomView

iphone - 从 iOS 4 切换到 5 时出现 NavigationBarStyle 问题

iphone - NSDateFormatter 给出了错误的小时(+8 小时)

iphone - 将新对象添加到 NSMutableArray

objective-c - 如何将日期+时间保存和检索到 sqlite3 数据库中?

iphone - Objective-C 2.0 : class_copyPropertyList(), 如何从类别中列出属性

ios - Swift 无法识别的选择器发送到实例错误

ios - 快速从 art.scnassets 中的文件夹加载 3D 模型