【问题标题】:how to properly update datasource when inserting and deleting rows from UITableView?从 UITableView 插入和删除行时如何正确更新数据源?
【发布时间】:2012-12-07 08:37:34
【问题描述】:

我正在尝试从 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];
    }

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

更新后现有节中包含的行数 (9) 必须等于该节中包含的行数 在更新 (9) 之前,加上或减去插入的行数或 从该部分删除(0 插入,1 删除)和加号或减号 移入或移出该部分的行数(0移入,0 搬出去了)。'

【问题讨论】:

    标签: iphone objective-c ios uitableview


    【解决方案1】:

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

    【讨论】:

    • 我尝试了您的建议,但现在消息已更改为...无效更新:无效的部分数。更新后表视图中包含的节数(8)必须等于更新前表视图中包含的节数(9),加上或减去插入或删除的节数(0插入,0已删除)。'
    • 错误消息报告您在更新 row 后的 sections 数量无效,这让我想知道这里是否存在第二个问题.无论如何,您要删除的行是该部分中唯一的行吗?
    【解决方案2】:

    您是否在正确的数组上调用 removeObjectAtIndex?根据错误消息,您实际上并没有从数据源中删除 tableview 期望您从中删除的对象。我猜你可能是从错误的数组中删除。

    还有一件事要尝试:先删除对象,然后执行 deleteRowsAtIndexPath。

    【讨论】:

    • 我尝试了您的建议,但现在消息已更改为...无效更新:无效的部分数。更新后表视图中包含的节数(8)必须等于更新前表视图中包含的节数(9),加上或减去插入或删除的节数(0插入,0已删除)。'
    • 修复它...愚蠢的错误。 (也返回了没有部分的响应......:X)
    【解决方案3】:

    你应该对部分使用方法

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

    【讨论】:

      猜你喜欢
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多