Refresh Only the Custom Header Views in a Uitableview

Refresh only the custom header views in a UITableView?

Instead of calling setNeedsDisplay, configure the header yourself by setting it's properties. And of course you have to get the actual headers in the table, don't call the delegate method, because that method usually creates a new header view.

I usually do this in a little helper method that is called from tableView:viewForHeaderInSection: as well.

e.g.:

- (void)configureHeader:(UITableViewHeaderFooterView *)header forSection:(NSInteger)section {
// configure your header
header.textLabel.text = ...
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"Header"];
[self configureHeader:header forSection:section];
}

- (void)reloadHeaders {
for (NSInteger i = 0; i < [self numberOfSectionsInTableView:self.tableView]; i++) {
UITableViewHeaderFooterView *header = [self.tableView headerViewForSection:i];
[self configureHeader:header forSection:i];
}
}

Is there a way to force a refresh of just a single header in UITableView?

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

Reload only one section header in UITableView

I'm a little unclear as to what's going on here, but it sounds like there is a UITableView concepts worth explaining here:

UITableView has its own concept of a cell, implemented as UITableViewCell, and its own concept of a header/footer, implemented as UITableViewHeaderFooterView.

Depending on which of these two you meant, there are a few things you can do to get the intended effect:

The UITableViewCell Approach:

If you're using a UITableViewCell as the first row of a section to act like a "header," and you just want to reload that row to the exclusion of the rest of the section, you can call yourTableViewInstance.reloadRows(at:with:) (Apple Documentation) This method takes an array of IndexPaths, and an animation style. You can pass in the indexPath of the one you want to reload.

The UITableViewHeaderFooterView Approach:

If you're using a proper UITableViewHeaderFooterView then you need to make sure that you're providing the appropriate view when reloading the section. Zack Shapiro outlines the steps you need to take in this answer:

  1. Create a class that's a subclass of UITableViewHeaderFooterView.
  2. Register it with your UITableView instance.
  3. Then in viewForHeaderInSection, you do let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderView") as! YourHeaderViewSubclass

The last thing he points out is this:

The deceptive thing is the function calls for a return of UIView? when it really needs a dequeuedReusableHeaderFooterView or reloadData will cause it to disappear.

It depends on which of these two implementation paths you're taking, but this should be enough information to point you in the right direction.

Edit:

Based on the code you added, it looks like you're calling yourTableViewInstance.dequeueReusableCell(withIdentifier:for:) instead of yourTableViewInstance.dequeueReusableHeaderFooterView(withIdentifier:) inside of viewForHeaderInSection.

You need to have a subclass of UITableViewHeaderFooterView and then call it correctly. Create that new subclass, then change this:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

let cellHeader = tableViewHome.dequeueReusableCell(withIdentifier: "header") as! HeaderTableViewCell

// ...

to this:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

let cellHeader = tableViewHome.dequeueReusableHeaderFooterView(withIdentifier: "header") as! HeaderTableView

// ...

You need to follow two steps here:

  1. Create a new class, subclassing UITableViewHeaderFooterView instead of UITableViewCell.
  2. Then use the appropriate class as outlined above.

How to reload a section header without it disappearing/re-appearing in Swift?

Found a solution referenced by @Abhinav:

Reload tableview section without scroll or animation

Slight modification updated for Swift 3.0:

UIView.performWithoutAnimation {

self.tableView.beginUpdates()
self.tableView.reloadSections(indexSet, with: UITableViewRowAnimation.none)
self.tableView.endUpdates()

}

Now if I scroll past the number of visible cells on screen and I delete a row, the headerCell.theCountLabel.text in my section header is updated without any animation, and remains still.

How to update a custom section header without reloading the whole table view?

try reloadSections method :

In Swift

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

self.tableView.reloadSections(indexSet, with: .automatic)

In Objective c

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation

How to reload a sectionHeader title in UITableView?

As far as I know you cant reload only sectionHeader.
The best you can do is to reload the section which contains that header using

self.tableView.reloadSections([1], with: .automatic)

where 1 is the index of section which you wanna refresh. Pass the index of whichever section you wanna reload.

Hope it helps

Changing UITableView section header without tableView:titleForHeaderInSection

There doesn't appear to be any standard API for accessing the system-provided section header view. Have you tried the more targeted reloadSections:withRowAnimation to get UIKit to display the new header text?

What kind of performance issues were you seeing with custom section header views? I doubt that the standard one is much more than just a UILabel.



Related Topics



Leave a reply



Submit