Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UITableView with a header area

I have a view that was created with all of the default UITableView stuff, but now I need to add a header area above where the UITableView is (so the UITableView will scroll normally, but the top 100px of the screen or so will have static header content). I don't see where I can resize the UITableView in IB, and am not sure how to do this.

Does anyone know?

like image 935
Dan Brown Avatar asked Jan 26 '10 22:01

Dan Brown


People also ask

How do you add a footer in tableView?

To create a basic header or footer with a text label, override the tableView(_:titleForHeaderInSection:) or tableView(_:titleForFooterInSection:) method of your table's data source object. The table view creates a standard header or footer for you and inserts it into the table at the specified location.

How do I see tableView headers in swift?

Show the table view, using the special section header functions. Set heightForHeaderInSection and viewForHeaderInSection.


1 Answers

You can use UITableViewDelegate methods to create a custom header view for a table and specify the height, namely tableView:viewForHeaderInSection: and tableView:heightForHeaderInSection:. You can add whatever you like to the view. Here's an example that adds a right aligned UILabel:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {      UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)];      UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, headerView.frame.size.width-120.0, headerView.frame.size.height)];      headerLabel.textAlignment = NSTextAlignmentRight;     headerLabel.text = [titleArray objectAtIndex:section];     headerLabel.backgroundColor = [UIColor clearColor];      [headerView addSubview:headerLabel];      return headerView;  }  -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {      return  30.0; } 
like image 141
noodl_es Avatar answered Sep 20 '22 01:09

noodl_es