Kobe Kobe - 1 month ago 8
iOS Question

Where to set UIRectCorner inside UITableViewCell

I want to apply some round corners to the bottom and top left of a view inside a table cell. And I made this extension on

UIView
:

extension UIView {
func round(_ corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}


But I am not sure where exactly to call this on my view to display it correctly. Right now I have it called in
layoutSubviews()
of my custom cell, but the bottom left part is not round when I start my app, only after the cell goes out of the screen and gets redrawn.

What should be a good place to set the round corners inside a
UITableViewCell
?

edit:

Here is the screenshot with the hierarchy:
enter image description here

Code:

override func layoutSubviews() {
super.layoutSubviews()

configureView()
}

func configureView() {
bgView.layer.cornerRadius = 3.0
period.round([.bottomLeft, .topLeft], radius: 3.0)
bgView.layer.masksToBounds = false
period.layer.masksToBounds = false
bgView.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor
bgView.layer.shadowOffset = CGSize(width: 0, height: 0)
bgView.layer.shadowOpacity = 0.8
}


edit 2:

enter image description here

Answer

This is my CustomTableViewCell class

import UIKit

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var period: UIView!
    @IBOutlet weak var bgView: UIView!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        bgView.layer.cornerRadius = 5.0

        // you might not be doing this earlier
        bgView.clipsToBounds = true

        period.round([.bottomLeft,.topLeft], radius: 5.0)

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

My TableView datasource methods

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
    return cell
}

enter image description here

My view hierarchy

enter image description here

Comments