UITableView: the proper way to display a separator for the last cell UITableView: the proper way to display a separator for the last cell ios ios

UITableView: the proper way to display a separator for the last cell


This worked flawlessly for me to add a separator to the last cell. have fun!

self.tableView.tableFooterView = [[UIView alloc] init];


When you add headerView or footerView to your TableView, last separator line will disappear.

Example below will let you make workaround for showing separator on the last cell. The only thing you have to implement more is to make this separator disappearing after selecting cell, so behavior is the same like in the rest of cells.

For Swift 4.0

override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {    let result = UIView()    // recreate insets from existing ones in the table view    let insets = tableView.separatorInset    let width = tableView.bounds.width - insets.left - insets.right    let sepFrame = CGRect(x: insets.left, y: -0.5, width: width, height: 0.5)    // create layer with separator, setting color    let sep = CALayer()    sep.frame = sepFrame    sep.backgroundColor = tableView.separatorColor?.cgColor    result.layer.addSublayer(sep)    return result}


I just found something that works for me. In a few words: give your UITableView a tableFooterView and set its frame's height to 0. This makes an actual separator show, with the right insets.

In more details: if you are using the storyboard, drag a UIView to the bottom of your Table View (in the tree view on the left) so it shows just below Table View Cell, at the same hierarchical level. This creates a tableFooterView. Of course this can be done programmatically as well.

Then, in your UITableViewController's implementation:

UIView *footerView = self.tableView.tableFooterView;CGRect footerFrame = footerView.frame;footerFrame.size.height = 0;[footerView setFrame:footerFrame];

Let me know if that works for you! It might also work for the first separator if you use a tableHeaderView instead, I haven't tried it though.