Set the corner radius of a cell in a viewview swift 2.0 table

I have a little problem, I want to change the radius of the cell as shown below.

enter image description here

+5
source share
3 answers

You can use the following code in swift 2.0

Put this code in the 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 code of my code

enter image description here

+11
source
 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 do we do if the section has only one line, then we do it from all sides, if the section has several lines, then we do it at the top in the first line and at the bottom of the last line ... properties BottomLeft, BottomRight, topLeft, TopRight should have rectangular rectangle (sentences from xcode when entering text ... there is another corner of the content with the same name .. so check this)

+9
source

It seems your tableView contains a UIView , so just add these rows to cellForRowAtIndexPath . If you do not add a UIView and add a radius to the UIView and just add this view to your cell ( cell.addSubView(YOURVIEW) ).

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

To set the border, you can

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

Update

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

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

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

+2
source

Source: https://habr.com/ru/post/1239146/


All Articles