3

In my iOS app, I have a table view and I would like to customise the border of my cells. I would like to reproduce an effect like the following:

enter image description here

I'm developing in Swift 2.0 for iOS 9.

giograno
  • 1,749
  • 3
  • 18
  • 30

4 Answers4

5

Tejas Ardeshna's answer would work, but it wont look good if borders are touching view bounds.

I would suggest add a view inside content view, keeping some padding. and then apply border to this view.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell", forIndexPath: indexPath) as UITableViewCell
    if cell.viewWithTag(100) == nil
    {
        let view = UIView()
        view.frame = CGRectInset(cell.contentView.bounds, 4 , 4)
        view.layer.borderWidth = 1
        view.layer.cornerRadius = 4
        view.layer.borderColor = UIColor(red: 0, green: 0, blue: 0.6, alpha: 1).CGColor
        cell.contentView.addSubview(view)
    }
    return cell;

}
Amit Tandel
  • 883
  • 7
  • 16
3

Try this code:

cell?.contentView.layer.cornerRadius = 4.0
cell?.contentView.layer.borderColor = UIColor.blueColor().CGColor
cell?.contentView.layer.borderWidth = 1.0
Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
1

You can add borderColor, borderWidth and cornerRadius to the contentView of the UITableViewCell

ElOjcar
  • 301
  • 2
  • 4
  • 12
Harshavardhan
  • 1,266
  • 2
  • 14
  • 25
0
ContentView.Layer.BorderColor = UIColor.LightGray.CGColor; 
Faheem
  • 930
  • 10
  • 7
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – HiDeoo Jul 12 '16 at 11:59