-->

Is there a way to force a refresh of just a single

2020-07-05 07:12发布

问题:

I have a UITableView where I want to force a refresh of just a specific section header row, not the data rows. For example, refresh the header for section 3. I know how to reload an entire section (header plus data), but can just the section header be refreshed?

EDIT: When data changes in one section of the table I want to update information in the header of a DIFFERENT section. Therefore, I need to get a reference to the section header and then refresh it.

回答1:

If the header is a custom UIView, then you can just call setNeedsDisplay on the UIView rather than on the UITableView.



回答2:

Well, in my case, after some asynchronous task i need to update my custom section header view.

and I get it to update it's height using : This Swift 3+ Code

//to reload your cell data
self.tableView.reloadData()
DispatchQueue.main.async {
// this is needed to update a specific tableview's headerview layout on main queue otherwise it's won't update perfectly cause reloaddata() is called
  self.tableView.beginUpdates()
  self.tableView.endUpdates()
}        


回答3:

UITableView does *not have any such method to reload only the header view/title ...You have to reload the whole section or the table.



回答4:

I beg to differ. I've run into this while deleting rows with custom section headers and (at least in iOS 6) you can simply call:

[self tableView:self.tableView viewForHeaderInSection:[indexPath section]];

And as long as you have proper code to return a view in that method, you can easily refresh the header for a specific section:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    if ([self tableView:tableView numberOfRowsInSection:section] == 0) {

        return nil; //hide the header if there are no rows...

    } else {
        // configure or refresh the header...
    }
}