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

后端 未结 14 927
礼貌的吻别
礼貌的吻别 2020-12-29 02:23

The question is what\'s the right-most way to display a separator in the last cell in a table/section.

Basically this is what I am after.

14条回答
  •  囚心锁ツ
    2020-12-29 03:06

    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
    }
    

提交回复
热议问题