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

前端 未结 29 1105
走了就别回头了
走了就别回头了 2020-11-29 15:02

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

29条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 15:41

    While thinking how to approach this problem, I remembered a very important detail about UITableViewStyleGrouped. The way UITableView implements the grouped style (the rounded borders around the cells) is by adding a custom backgroundView to the UITableViewCells, and not to the UITableView. Each cell is added a backgroundView according to its position in the section (upper rows get the upper part of the section border, middle ones get the side border and the bottom one gets – well, the bottom part). So, if we just want a plain style, and we don’t have a custom backgroundView for our cells (which is the case in 90% of the times), then all we need to do is use UITableViewStyleGrouped, and remove the custom background. This can be done by following those two steps:

    Change our tableView style to UITableViewStyleGrouped Add the following line to cellForRow, just before we return the cell:

    cell.backgroundView=[[[UIView alloc] initWithFrame:cell.bounds] autorelease];

    And that’s it. The tableView style will become exactly like UITableViewStylePlain, except for the floating headers.

    Hope this helps!

提交回复
热议问题