0

I have a custom UIView containing a TableView. Above the table I plan to display some other items. For now, I just want a button.

I am creating a UIView 'headerView', and adding the button to it. The button itself is just rigged to update it's title when it is pressed.

If I add the headerView to the table view as the tableHeaderView, it works just fine.

If I add the headerView above the tableview, as a sibling subview, it displays correctly but the button title does not update.

I have verified through logging that the method to update the button label is called, but the view seems to need a refresh.

Why would this be? Am I missing something simple.

Code to follow if it would help.

Note: I am not using IB for any of this.

Update:I have narrowed it down to one line. When I set the frame of the tableview, which I add below the headerView, I am setting the origin.y to the headerView.size.height. If I change this to use a hard coded number (even of the same value), the button updates.

Ben Packard
  • 26,102
  • 25
  • 102
  • 183

1 Answers1

1

It's hard to diagnose the issue without any code, but here are a few common issues/solutions that might help.

  1. Use [button setTile:forState:] as opposed to button.titleLabel.text
  2. Make sure you aren't missing any calls to layoutSubviews by calling setNeedsLayout and/or layoutIfNeeded on appropriate view.
  3. Make sure you have the correct selector names hooked to the right target with the right control event.

Sounds like since it works in some cases, 1 and 3 aren't the issue. Also, since it works when you set the size differently it could be 2. The tableview would be calling layout methods correctly to it's header view, which might be why it works as the table header view and not as a normal sibling.

Matt
  • 1,586
  • 8
  • 12