I have small problem , I want to change cell corner radius like below image
-
i want to set corner like above image section 1 – John Dec 24 '15 at 15:36
-
I don't thing this is possible on iOS – Fabio Berger Dec 24 '15 at 15:43
3 Answers
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 )
- 406
- 6
- 16
-
This should be marked as the Correct Answer, at least it's what helped me :-) – Hernan Arber Jun 12 '17 at 13:12
-
@SaiPavanParanam thank you so much, after searching for days i lost hope and finally your solution works with slight modification to my scenario. – Arshad Shaik Sep 26 '18 at 12:08
-
-
my app never gets inside the willDisplayCell. What may be the problem? – Mert Köksal Mar 24 '22 at 12:09
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
- 37,198
- 7
- 99
- 103
- 288
- 3
- 18
-
If that doesn't work try `cell!.contentView.layer`. I suspect that it's the cell's content view that has the border and rounded corners, not the cell. – Duncan C Dec 24 '15 at 15:47
-
@DuncanC Please see my edited answer. this code is worked for me. – Pallavi Nikumbh Dec 24 '15 at 16:27
-
It looks like the OP wants a sectioned table view where the sections are grouped with a rounded rectangle around the whole section. That's more complicated. – Duncan C Dec 24 '15 at 16:34
-
I want to radius section of tableview cell not row of tableview cell see image above – John Jan 06 '16 at 09:34
-
-
@SaiPavanParanam See the difference between both the code. you will get your answer. :) – Pallavi Nikumbh Jun 23 '17 at 11:20
-
@PallaviNikumbh Your answer does not actually solve his question. – SaiPavanParanam Jun 29 '17 at 10:03
-
@PallaviNikumbh You are actually giving border to the entire Cell. He just needs it the section wise. – SaiPavanParanam Jun 29 '17 at 10:05
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.
- 37,198
- 7
- 99
- 103
-
-
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
-
-
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