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?
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.
Show the table view, using the special section header functions. Set heightForHeaderInSection and viewForHeaderInSection.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With