How to add a custom separator to UITableViewCell?

后端 未结 4 1168
北海茫月
北海茫月 2020-12-16 17:22

Please spare sometime as this is a long explanation

I have a UIViewController which consists of a UIButton and a UITableView w

4条回答
  •  天涯浪人
    2020-12-16 17:41

    As others said, usually there's two ways to do this, either create a CALayer or a UIView separator of 1px and add it to contentView.

    Over time I've seen multiple projects do this differently, and sometimes, even multiple different ways in the same project. It's also easy to introduce bugs because of cell reuse and also, for proper rendering of pixel lines, one must incorporate the screen scale: (1.0 / [UIScreen mainScreen].scale).

    I've created a library that simplifies this to just a single method class, with no subclassing required. https://github.com/kgaidis/KGViewSeparators

    Objective-C:

    [view kg_show:YES separator:KGViewSeparatorTop color:[UIColor blackColor] lineWidth:KGViewSeparatorLineWidth(1.0) insets:UIEdgeInsetsMake(0, 15.0, 0, 15.0)];

    Swift:

    view.kg_show(true, separator: .Bottom, color: UIColor.blackColor(), lineWidth: KGViewSeparatorLineWidth(1.0), insets: UIEdgeInsetsZero)

提交回复
热议问题