11

I have looked everywhere. Trying to increase the thickness of this line. Is there anyway to do this programmatically? Thanks

Kex
  • 8,023
  • 9
  • 56
  • 129

4 Answers4

13

The only way of doing it, is setting the separtorStype to UITableViewCellSeparatorStyleNone and then you have two options:

  • Create a custom UITableViewCell with the separator inside it or
  • Create an alternate UITableViewCell with the separator you want and place inside every other cells. If you want to display 3 rows on your table, you should display 5 instead with the alternate cell in rows 2 and 4.
jherran
  • 3,337
  • 8
  • 37
  • 54
  • or... you could just add a blank cell (with a fixed height) after adding a cell. – Fredrik Johansson Apr 13 '16 at 16:01
  • this answer is true and worked fine , in this link you can find code example https://stackoverflow.com/questions/39571799/show-two-different-custom-cells-in-same-uitableview-swift-firebase – mahdi Aug 12 '17 at 06:23
6
private let kSeparatorId = 123
private let kSeparatorHeight: CGFloat = 1.5

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
    if cell.viewWithTag(kSeparatorId) == nil //add separator only once
    {
        let separatorView = UIView(frame: CGRectMake(0, cell.frame.height - kSeparatorHeight, cell.frame.width, kSeparatorHeight))
        separatorView.tag = kSeparatorId
        separatorView.backgroundColor = UIColor.redColor()
        separatorView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

        cell.addSubview(separatorView)
    }
}
ChikabuZ
  • 10,031
  • 5
  • 63
  • 86
  • 1
    My cells can be different height which caused issues with this excellent solution as the user scrolled. To fix this, I replaced the if statement with the following code: cell.viewWithTag(kSeparatorId)?.removeFromSuperview() – Justin Domnitz Sep 27 '17 at 19:38
1

My solution to this was to add an extension to either UIView or a UITableViewCell.

extension UIView {

func addSeparator(ofHeight height : CGFloat) {
    let lineView = UIView()
    lineView.backgroundColor = .red
    self.addSubview(lineView)
    let constraintString = "V:|-\(self.frame.size.height - height)-[v0(\(height))]|"
    self.addConstraintsWithFormat("H:|[v0]|", views: lineView)
    self.addConstraintsWithFormat(constraintString, views: lineView)
}

//MARK: - Constraints Extension

func addConstraintsWithFormat(_ format: String, views: UIView...) {
    var viewsDictionary = [String: UIView]()
    for (index, view) in views.enumerated() {
        let key = "v\(index)"
        view.translatesAutoresizingMaskIntoConstraints = false
        viewsDictionary[key] = view
    }
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
} }

Then use it within your custom TableViewCell or any View you would like to add a bottom line to.

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    self.addSeparator(ofHeight: 1)
}
Russell Warwick
  • 91
  • 1
  • 10
0

Set the table separatorsType to UITableViewCellSeparatorStyleNone and configure a cell backroundView with bg color as you like it for the separators and a subview with which to mask it as much as you want. Something like this:

UIView * bg = [[UIView alloc] initWithFrame:cell.bounds];
bg.backgroundColor = [UIColor darkGrayColor];
bg.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

UIView * overBg = [[UIView alloc] initWithFrame:CGRectInset(cell.bounds, 0, 4.)];
overBg.backgroundColor = [UIColor whiteColor];
overBg.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

[bg addSubview:overBg];
cell.backgroundView = bg;
Jim75
  • 737
  • 8
  • 13