UITableView reload section

前端 未结 11 2054
余生分开走
余生分开走 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: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];
    }
    

提交回复
热议问题