6

I have small problem , I want to change cell corner radius like below image

enter image description here

adolfosrs
  • 8,978
  • 5
  • 38
  • 65
John
  • 183
  • 2
  • 3
  • 14

3 Answers3

16
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
if (tableView == self.orderDetailsTableView)
{
    //Top Left Right Corners
    let maskPathTop = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.TopLeft, .TopRight], cornerRadii: CGSize(width: 5.0, height: 5.0))
    let shapeLayerTop = CAShapeLayer()
    shapeLayerTop.frame = cell.bounds
    shapeLayerTop.path = maskPathTop.CGPath

    //Bottom Left Right Corners
    let maskPathBottom = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.BottomLeft, .BottomRight], cornerRadii: CGSize(width: 5.0, height: 5.0))
    let shapeLayerBottom = CAShapeLayer()
    shapeLayerBottom.frame = cell.bounds
    shapeLayerBottom.path = maskPathBottom.CGPath

    //All Corners
    let maskPathAll = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.TopLeft, .TopRight, .BottomRight, .BottomLeft], cornerRadii: CGSize(width: 5.0, height: 5.0))
    let shapeLayerAll = CAShapeLayer()
    shapeLayerAll.frame = cell.bounds
    shapeLayerAll.path = maskPathAll.CGPath

    if (indexPath.row == 0 && indexPath.row == tableView.numberOfRowsInSection(indexPath.section)-1)
    {
        cell.layer.mask = shapeLayerAll
    }
 else if (indexPath.row == 0)
    {
    cell.layer.mask = shapeLayerTop
    }
    else if (indexPath.row == tableView.numberOfRowsInSection(indexPath.section)-1)
    {
        cell.layer.mask = shapeLayerBottom
    }
}
}

what actually we are doing is if section has only one row then we do it on all sides, if section has multiple rows then we do it top on first row and bottom at last row... the properties BottomLeft, BottomRight, topLeft, TopRight should be of type rect corner(Suggestions from xcode when you are typing... there is another property content corner with same name.. so check that )

SaiPavanParanam
  • 406
  • 6
  • 16
13

You can use following code in swift 2.0

Put this code in cellForRowAtIndexpath method:

cell!.layer.cornerRadius=10 //set corner radius here
cell!.layer.borderColor = UIColor.blackColor().CGColor  // set cell border color here
cell!.layer.borderWidth = 2 // set border width here

Below is the output of my code

enter image description here

Rashwan L
  • 37,198
  • 7
  • 99
  • 103
Pallavi Nikumbh
  • 288
  • 3
  • 18
3

It seems that your tableView contains an UIView so just add these rows in cellForRowAtIndexPath. If not add an UIView and add the radius to the UIView and just add that view to your cell (cell.addSubView(YOURVIEW)).

cell.contentView.layer.cornerRadius = 10
cell.contentView.layer.masksToBounds = true

To customize the border you can

cell.layer.borderColor = UIColor.grayColor().CGColor
cell.layer.borderWidth = 5

Update

To add this in your viewForHeaderInSection Create a view let view = UIView() and add the radius to your view

view.layer.cornerRadius = 10
view.layer.masksToBounds = true

And add other properties that you need and return that view.

Rashwan L
  • 37,198
  • 7
  • 99
  • 103
  • it's set whole cell corner radius i want to set section wise – John Dec 24 '15 at 15:55
  • Then you need to add a viewForHeaderInSection to your tableView if you don´t already use it. Check the updated code. – Rashwan L Dec 24 '15 at 15:59
  • i also try http://stackoverflow.com/questions/18822619/ios-7-tableview-like-in-settings-app-on-ipad but not work – John Dec 24 '15 at 16:18
  • Add your code to your question, then it´s easier to help you. – Rashwan L Dec 24 '15 at 16:23
  • 3
    "but not work" is **NOT** helpful at all. You need to describe what you tried in detail, as well as how it did not meet your needs, *also in detail*. – Duncan C Dec 24 '15 at 16:36