Adding a header to UITableview programmatically

That's just a header in the TableView, they appear on top of your section

Use this method if you want a title:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

Or this one if you want a custom view:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

And for the height:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

Updated For Swift 4

Similar to SimplePJ's answer. If you want to create a custom header view for the ENTIRE TABLE VIEW you can create one and set it as the tableHeaderView like so.

  let headerView: UIView = UIView.init(frame: CGRect(x: 1, y: 50, width: 276, height: 30))
  headerView.backgroundColor = .red

  let labelView: UILabel = UILabel.init(frame: CGRect(x: 4, y: 5, width: 276, height: 24))
  labelView.text = "My header view"

  headerView.addSubview(labelView)
  self.tableView.tableHeaderView = headerView

This is not to be confused with creating a table view section header in which you'd use the following method.

func headerView(forSection section: Int) -> UITableViewHeaderFooterView?

The return value is the header view associated with the section, or nil if the section does not have a header view.


Try This one Programmatically adding header it's Easy..

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(1, 50, 276, 30)];
headerView.backgroundColor = [UIColor colorWithRed:235/255.0f green:235/255.0f blue:235/255.0f alpha:1.0f];

UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(4, 5, 276, 24)];
labelView.text = @"hello";

[headerView addSubview:labelView];
self.tableView.tableHeaderView = headerView;

Or Try in Swift

var headerView: UIView = UIView.init(frame: CGRectMake(1, 50, 276, 30))
headerView.backgroundColor = UIColor(red: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1.0)

var labelView: UILabel = UILabel.init(frame: CGRectMake(4, 5, 276, 24))
labelView.text = "hello"

headerView.addSubview(labelView)
self.tableView.tableHeaderView = headerView

Swift 4.0 and later

var headerView: UIView = UIView.init(frame: CGRect.init(x: 1, y: 50, width: 276, height: 30))
headerView.backgroundColor = UIColor(red: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1.0)

var labelView: UILabel = UILabel.init(frame: CGRect.init(x: 4, y: 5, width: 276, height: 24))
labelView.text = "hello"

headerView.addSubview(labelView)
self.tableView.tableHeaderView = headerView

Just implement these two delegate methods in your TableViewController

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

In the first method return the height of the header view, In the second return the view that should be displayed

here is an example

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 55.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"simpleHeader.png"]];
}

Tags:

Ios