How to customize tableView separator in iPhone

前端 未结 12 1988
夕颜
夕颜 2020-11-28 20:49

By default there is a single line separator in uitableview.

But I want to put my customized line as a separator.

Is it possible? How?

12条回答
  •  醉话见心
    2020-11-28 21:26

    If you are using Custom Cells in Swift: An alternative approach is to extend UITableViewCell with a function that can draw a line at the top of that cell.

    import UIKit
    
    extension UITableViewCell {
        func addSeparatorLineToTop(){
            let lineFrame = CGRectMake(0, 0, bounds.size.width, 1)
            let line = UIView(frame: lineFrame)
            line.backgroundColor = UIColor.myGray_300()
            addSubview(line)
        }
    }
    

    Then you can add this line to any custom cell, for example in the awakeFromNib

    override func awakeFromNib() {
        super.awakeFromNib()
        addSeparatorLineToTop()
    }
    

    This solution is nice because it does not mess up your storyboard and limits your extra code to 1 line.

提交回复
热议问题