【问题标题】:insertSections in tableView表格视图中的插入部分
【发布时间】:2014-05-16 20:40:19
【问题描述】:

我想在更新后插入一些部分,但我有相同的元素。我该怎么做?我想在最后一个元素之后插入元素。但是有错误。

我的代码

     [self.tableView beginUpdates];
     for (WallPost *wp in news) {
        if (!_tableDataSource.count || ![_tableDataSource containsObject:wp]) {
           [_tableDataSource insertObject:wp atIndex:_tableDataSource.count];
           [self.tableView insertSections:
               [NSIndexSet indexSetWithIndex:
               _tableDataSource.count-1] 
               withRowAnimation:UITableViewRowAnimationBottom];
        }
     }
     [self.tableView endUpdates];

【问题讨论】:

  • 你遇到了什么错误?

标签: ios uitableview tableview


【解决方案1】:

我不知道您到底有什么错误,但您在for 循环中的tableView 中插入section。相反,您应该将对象添加到数组中,然后通过indexSetWithIndexesInRange 方法使用一组索引重新加载tbaleView 中的部分

int count = 0;
for (WallPost *wp in news) {
    if (!_tableDataSource.count || ![_tableDataSource containsObject:wp]) {
        [_tableDataSource addObject:wp];
        count++;
    }
}

if(count > 0) {
    [self.tableView beginUpdates];
    [self.tableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(_tableDataSource.count-count, count) withRowAnimation:UITableViewRowAnimationBottom];
    [self.tableView endUpdates];
}

只是一份报告:

你不需要使用:

[_tableDataSource insertObject:wp atIndex:_tableDataSource.count];

因为您每次都在末尾添加对象..所以更喜欢:

[_tableDataSource addObject:wp];

【讨论】:

  • 当您添加其他部分时,您应该使用 [self.tableView reloadSections:[Range] 和 Animation:Animation] 来重新加载和插入变体。
猜你喜欢
  • 2012-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-05
  • 1970-01-01
相关资源
最近更新 更多