UITableView重新加载部分

Ani*_*ari 40 iphone uitableview

我想只重新加载一个部分而不是整个表.有什么方法吗UITableView

[tableView reloadData]用于加载全表.
我想知道如何只加载一个部分,因为我在表中有大量的行.

ban*_*isa 69

reloadSections方法让我感到困惑 - 因为我必须构造一些对象.如果您需要灵活性,这很好,但有时我也只是想要简单.它是这样的:

NSRange range = NSMakeRange(0, 1);
NSIndexSet *section = [NSIndexSet indexSetWithIndexesInRange:range];                                     
[self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone];
Run Code Online (Sandbox Code Playgroud)

这将重新加载第一部分.我更喜欢在UITableView上有一个类别,只需调用此方法:

[self.tableView reloadSectionDU:0 withRowAnimation:UITableViewRowAnimationNone];
Run Code Online (Sandbox Code Playgroud)

我的类别方法如下所示:

@implementation UITableView (DUExtensions)

- (void) reloadSectionDU:(NSInteger)section withRowAnimation:(UITableViewRowAnimation)rowAnimation {
    NSRange range = NSMakeRange(section, 1);
    NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];                                     
    [self reloadSections:sectionToReload withRowAnimation:rowAnimation];
}
Run Code Online (Sandbox Code Playgroud)

  • 或者你可以做`[NSIndexSet indexSetWithIndex:1]`(而不是范围) (5认同)

sos*_*orn 35

就在这里:

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
Run Code Online (Sandbox Code Playgroud)


小智 24

但是,您只能重新加载包含相同行数的 部分(或者您必须手动添加或删除它们).否则你会得到:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 2. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

使用时不需要[tableView reloadData].

当你需要重新加载一个部分并且你已经改变了它内部的行数时,你可以使用这样的东西:

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];

[self beginUpdates];
    [self deleteSections:indexSet withRowAnimation:rowAnimation];
    [self insertSections:indexSet withRowAnimation:rowAnimation];
[self endUpdates];
Run Code Online (Sandbox Code Playgroud)

如果你把它放在一个类别(如bandejapaisa节目),它可能看起来像这样:

- (void)reloadSection:(NSInteger)section withRowAnimation:(UITableViewRowAnimation)rowAnimation {
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];

    [self beginUpdates];
        [self deleteSections:indexSet withRowAnimation:rowAnimation];
        [self insertSections:indexSet withRowAnimation:rowAnimation];
    [self endUpdates];
}
Run Code Online (Sandbox Code Playgroud)


pab*_*ros 14

对于Swift 3和Swift 4

let sectionToReload = 1
let indexSet: IndexSet = [sectionToReload]

self.tableView.reloadSections(indexSet, with: .automatic)
Run Code Online (Sandbox Code Playgroud)


Ofi*_*chi 6

那个正确的方法:

[self.tableView beginUpdates]; 
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)