-2

I am trying to add a custom header for tableview (not for section). I see that this post shows how to do that: Table Header Views in StoryBoards

can I add controls like UISwitch, UIButton and UILabel to the view in tableheader so they can be hidden or updated dynamically?

I missed an important point, which is, I'd like this view to stay at top even though i scroll the rows. so this header will always show the total. if I delete a row, the total gets updated right on the screen. also a button and switch so user can choose certain things. appreciate if anyone could point me to some example.

I tried adding a view as mentioned in above URL but that UIView goes out of view when i scroll the table down.

Sounds like what I need is the layout of UIView, UITableView embedded in UIViewController. Pl see the attached image. How do I handle the events for UIView and UITableView in a subclass of UIViewController?

"header" that stays on top

thank you

Community
  • 1
  • 1
user3282227
  • 131
  • 2
  • 10
  • 1
    What do you mean by "dynamically" ? The header view is a simple view. – GaétanZ Sep 01 '15 at 16:43
  • possible duplicate of [Table Header Views in StoryBoards](http://stackoverflow.com/questions/7841167/table-header-views-in-storyboards) – Jaideep Nagra Sep 01 '15 at 17:05
  • @Poql: I plan to have a "Total:" label that needs to be updated based on the quantity of each item in the table below. Also a UIbutton that needs to be hidden or not based on if app can reach server. if i subclass UIView, do I get to write handler methods? I prefer to go storyboard way. – user3282227 Sep 01 '15 at 17:14

2 Answers2

2

Yes you can add any UIView or subclass as a tableViewHeader, either via storyboard or programmatically via:

UIView *someView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,100)];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10,10,300,80)];
[someView addSubview:button];
self.tableView.tableHeaderView = someView;

Where your view can contain any other view.

Or even

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10,10,300,80)];
self.tableView.tableHeaderView = button;

for only adding a UIButton. Nevertheless, the view you set as tableHeaderView will fill the whole width of your surrounding UITableView, while the height will stay as specified in your frame.

MarkHim
  • 5,686
  • 5
  • 32
  • 64
0

Yes, you can add whatever type of view that you want to the table header. Create a view that has your Switch, Button, etc on it either in a xib or programmatically, then use that view and assign it to the tableHeaderView of the UITableView (can be referenced from the tableView property of a UITableViewController).

See here for an example: Adding iOS UITableView HeaderView (not section header)

Community
  • 1
  • 1
N8P
  • 393
  • 1
  • 4
  • 11