UITableView reload section

前端 未结 11 1884
余生分开走
余生分开走 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:19

    Based on the accepted answer here, I made a function that will reload all sections in the table using an animation. This could probably be optimized by reloading only visible sections.

    [self.tableView reloadData];
    NSRange range = NSMakeRange(0, [self numberOfSectionsInTableView:self.tableView]);
    NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
    [self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationFade];
    

    In my case, I had to force a reloadData before the section animation, because the underlying data for the table had changed. It animates properly however.

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

    You need this... For Reload Row

    - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    

    or For Reload section

    - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
    
    0 讨论(0)
  • 2020-12-05 02:23

    Try to use

    [self.tableView beginUpdates]; 
    [self.tableView endUpdates];
    

    Hope this will solve your issue.

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

    Yes, there is:

    - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
    
    0 讨论(0)
  • 2020-12-05 02:26

    If you have custom section view you can add a weak reference to it in your view controller and update it whenever you want. Here is my code for reference:

    @property (weak, nonatomic) UILabel *tableHeaderLabel;
    
    ....
    
    -(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UITableViewHeaderFooterView *myHeader = [[UITableViewHeaderFooterView alloc] init];
    
        UILabel *titleLabel = [[UILabel alloc] init];
        [titleLabel setFrame:CGRectMake(20, 0, 280, 20)];
        [titleLabel setTextAlignment:NSTextAlignmentRight];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        [titleLabel setFont:[UIFont systemFontOfSize:12]];
    
        [myHeader addSubview:titleLabel];
    
        self.tableHeaderLabel = titleLabel; //save reference so we can update the header later
    
        return myHeader;
    }
    

    Then later on you can update your section like this:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        self.tableHeaderLabel.text = [NSString stringWithFormat:@"Showing row: %ld", indexPath.row];
    }
    
    0 讨论(0)
  • 2020-12-05 02:27

    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 an NSInternalInconsistencyException.

    Steps:

    1. calculate which rows to remove and/or insert
    2. generate an IndexPath array from these
    3. call related tableView methods

    now you can safely call reloadSections :) Reload section will call update for the rest of the indexes.

    Or you can use a library like : https://github.com/onmyway133/DeepDiff

    Swift pseodo code:

    
            tableView.deleteRows(at: valueIndexesToRemove, with: .automatic)
            tableView.insertRows(at: valueIndexesToInsert, with: .automatic)
            tableView.reloadSections(IndexSet([section]), with: .automatic)
    
    
    0 讨论(0)
提交回复
热议问题