Is it possible to disable floating headers in UITableView with UITableViewStylePlain?

I’m using a UITableView to layout content ‘pages’. I’m using the headers of the table view to layout certain images etc. and I’d prefer it if they didn’t float but stayed static as they do when the style is set to UITableViewStyleGrouped.

Other then using UITableViewStyleGrouped, is there a way to do this? I’d like to avoid using grouped as it adds a margin down all my cells and requires disabling of the background view for each of the cells. I’d like full control of my layout. Ideally they’d be a “UITableViewStyleBareBones”, but I didn’t see that option in the docs…

Many thanks,

31 Answers
31

A probably easier way to achieve this:

Objective-C:

CGFloat dummyViewHeight = 40;
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, dummyViewHeight)];
self.tableView.tableHeaderView = dummyView;
self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 0, 0, 0);

Swift:

let dummyViewHeight = CGFloat(40)
self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: dummyViewHeight))
self.tableView.contentInset = UIEdgeInsets(top: -dummyViewHeight, left: 0, bottom: 0, right: 0)

Section headers will now scroll just like any regular cell.

Leave a Comment