Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView reloadSections/reloadData: header view becomes blank

I have a UITableView with some sections, each has its own header view. When user taps on the header view of a section, all rows of that section will collapse. What i do is, I set the number of row of that section to 0, and then call :

[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationBottom];

Everything works as expected, except one thing : the header view of the section becomes white blank. When i scroll the table, then the header becomes normal again.

So i guess there's some problem with the drawing of the table.

One funny thing is, if i use UITableViewRowAnimationFade instead, then even when i scroll the table, the header is still white blank.

When I update just ONE section there is also no problem - when I update more than one section the problem occurs.

If i use

[self.tableView reloadData]

instead, then everything works fine.

The reason i use

[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationBottom];

is because i want animation.

Wrapping with beginUpdates / endupdates does not work.

like image 394
Dominic Sander Avatar asked Dec 28 '11 15:12

Dominic Sander


2 Answers

I realize that it's a LONG time since this question was posed but I think all of the answers given below are incorrect.

I had the same problem (with someone else's code) and was about to be fooled by this post when I realized that the code was not doing it's reuse of the table header correctly. The header disappearing was because the code was only supplying a UIView, not a UITableViewHeaderFooterView, registered correctly and set up for reuse.

Have a look at the answer here: How to use UITableViewHeaderFooterView?

My blank headers went away when I set up a reusable header.

like image 91
Darren Avatar answered Nov 13 '22 15:11

Darren


In Swift:

You need to create a class that's a subclass of UITableViewHeaderFooterView and register it to the table view. Then in viewForHeaderInSection, you do let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderView") as! YourHeaderView, similar to what you do for UITableViewCells. Then return that header.

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

like image 3
Zack Shapiro Avatar answered Nov 13 '22 14:11

Zack Shapiro