0

I am new to iOS programming, I have a requirement where I need to display a custom header section for the table. It should look similar to

enter image description here

I checked what API/method is responsible for showing the header and found

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"HEADER";
}

Question

  • Do I need to build a separate UITableViewCell? and how do I stitch it then with header?

Please recommend

Thank you

daydreamer
  • 87,243
  • 191
  • 450
  • 722

2 Answers2

0

You don't have to build a separate UITableViewCell. If you need a custom tableView header, build your custom UIView and then set it as the tableView's header.

self.tableView.tableHeaderView = customView;

Or if you need to set tableView's section header to a custom view:

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
    //Add UILabels to sectionView, style it, etc.

    return sectionView;
}
Sebyddd
  • 4,305
  • 2
  • 39
  • 43
0

You can do:

tableView:viewForHeaderInSection: 

Where the returning value is a UIView

tableView:heightForHeaderInSection:

Where the returning value is a CGFloat

Alternatively you can set:

[self.tableView setTableHeaderView:viewForHeader];

Where viewForHeader is your custom view.

Note: Both of them behave different. if you user HeaderInSection the header will scroll within the section until it collides with the next header.

If you set the TableHeaderView it will dock always at the top of your tableView it will depend of what you want to accomplish.

artud2000
  • 544
  • 4
  • 9