itripn
itripn

Reputation: 84

UIButton in UITable Section Header Not Updating

I have a simple UITableView with two sections. Each section has a header which is displaying correctly.

In the second section, it's header has a UIButton in it (as well as a UILabel) -- the view for the section header is loaded from a xib -- again, it views fine and the button reacts to tapping and calls my IBAction.

Each table cell in this section also has a button. When one of these buttons is tapped, I update the text in the button in the section header with a count. For background, each table cell represents a contact, and tapping the button in a given cell adds that contact to a list, and tapping the button in the section header will perform an action on all the contacts in the list.

Here is the crux -- when the tableview first displays, tapping the button in cells correctly updates the section header button text right away. Once I scroll the table, even a little, and tap buttons, the section header button text no longer updates.

If I give the table a hard scroll up such that the section header bangs against the top or such, the section header button label suddenly updates, as if redraw events have been queued up and suddenly are released.

I've tried performing the update on the main thread, in an async dispatch block, calling setNeedsDisplay/Layout, etc. It refuses to update unless I wack the table around.

Calling reloadData on the table after updating the button text works, but that seems awful heavy handed.

Thanks for any insights...

The code updating the button text:

   NSString *l = NSLocalizedString(@"INVITE_LABEL", nil);
   NSString *label = [NSString stringWithFormat:l, [invitedEmails count]];
   [inviteSelectedButton setTitle:label forState:UIControlStateNormal];

Calling that from the IBAction of a table view button tap event. If I suffix that code with:

   [self.viewController.addressBookTable reloadData];

the code above it works right away every time.

Upvotes: 0

Views: 358

Answers (1)

AE'G-wiz
AE'G-wiz

Reputation: 46

I've had similar issues before with buttons inside of table views - cells or headers. In my case it was always a situation where even though I believed I was correctly targeting the desired Button View, with scrolling or table reloads, the button was actually being replaced with a new instance. Without going into too much detail, my solution was to create a custom button that subclassed UIButton and responded appropriately to my actions by calling methods directly on the (id)sender or by posting UINotifications.

I'm not sure if this will help you with your problem, but perhaps it will trigger a solution.

Upvotes: 3

Related Questions