UITableView reload section

前端 未结 11 1885
余生分开走
余生分开走 2020-12-05 01:46

I want to reload only one section not the full table. Is there any method in UITableView.

[tableView reloadData] is used to load full table

相关标签:
11条回答
  • 2020-12-05 02:28

    For Swift 3, 4 and 5

    let sectionToReload = 1
    let indexSet: IndexSet = [sectionToReload]
    
    self.tableView.reloadSections(indexSet, with: .automatic)
    
    0 讨论(0)
  • 2020-12-05 02:35

    But you can reload only sections which contain same number of rows (or you have to manually add or remove them). Otherwise you will get:

    *** 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).'

    Which is not required when you use [tableView reloadData].

    When you need to reload a section and you have changed number of rows inside it, you could use something like this:

    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];
    
    [self beginUpdates];
        [self deleteSections:indexSet withRowAnimation:rowAnimation];
        [self insertSections:indexSet withRowAnimation:rowAnimation];
    [self endUpdates];
    

    If you put it in a category (like bandejapaisa shows) it could look like this:

    - (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];
    }
    
    0 讨论(0)
  • 2020-12-05 02:39

    The reloadSections method bugs me -- as I have to construct a few objects. This is great if you need the flexibility, but sometimes I also just want the simplicity too. It goes like this:

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

    This will reload the first section. I prefer to have a category on UITableView and just call this method:

    [self.tableView reloadSectionDU:0 withRowAnimation:UITableViewRowAnimationNone];
    

    My category method looks like this:

    @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];
    }
    
    0 讨论(0)
  • 2020-12-05 02:40

    Here is the method, you can pass section details in different ways

    [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:1] withRowAnimation:NO];
    
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
    

    Reloading particular sections improves performance for the table view as well some time it also avoid some issues like floating/moving custom headers-footers in your view. SO try to use reloadSection than relaodData whenever possible

    0 讨论(0)
  • 2020-12-05 02:44

    that the correct way:

    [self.tableView beginUpdates]; 
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView endUpdates];
    
    0 讨论(0)
提交回复
热议问题