User Table Header Section

I have an application with a group style table.

I tried to set up the table header section:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)]; UILabel *userName = [[UILabel alloc] initWithFrame:CGRectMake (0,0,200,30)]; if (section != 0)userName.text = @"lalala"; [userName setFont:[UIFont fontWithName:@"HelveticaNeue" size:20]]; [headerView addSubview:userName]; [headerView setBackgroundColor:[UIColor clearColor]]; [userName setBackgroundColor:[UIColor clearColor]]; return headerView; } 

But my headers close the cells:

enter image description here

Why?

+4
source share
2 answers

You also need to implement tableView:heightForHeaderInSection: to set the correct height for the section title. See Apple Docs

+16
source

This is because the height of your header is set to 0 until you override another delegate:

- tableView: heightForHeaderInSection:

+5
source

Source: https://habr.com/ru/post/1401819/


All Articles