JuicyFruit JuicyFruit - 2 months ago 6x
iOS Question

tableViewCell.roundCorners are not shown immediately

I have these methods for collectionViewCell

cell.restarauntImage.roundCorners([.TopLeft, .TopRight], radius: 10, borderColor: UIColor.clearColor(), borderWidth: 0)

and tableViewCell

cell.restImage.roundCorners([.TopLeft, .TopRight], radius: 7, borderColor: UIColor.clearColor(), borderWidth: 0)

the problem is that it works perfectly with collectionView, but in tableView it is not working instantly with .TopRight, it applies only if I reuse cell several times, however .TopLeft works. Also if I remove .TopLeft and try to apply only to .TopRight it doesn't work either. What might be the problem?

update: extension found on stack overflow

extension UIView {
func roundCorners(corners:UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat)
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.CGPath
self.layer.mask = mask
addBorder(mask, borderWidth: borderWidth, borderColor: borderColor)

private func addBorder(mask: CAShapeLayer, borderWidth: CGFloat, borderColor: UIColor) {
let borderLayer = CAShapeLayer()
borderLayer.path = mask.path
borderLayer.fillColor = UIColor.clearColor().CGColor
borderLayer.strokeColor = borderColor.CGColor
borderLayer.lineWidth = borderWidth
borderLayer.frame = bounds

update2: cellForRowAtIndexPath, I've tried to put this method to each case after ink_setImage, but it didn't work either.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("searchCell" , forIndexPath: indexPath) as! SearchTableViewCell
cell.restImage.roundCorners([.TopLeft, .TopRight], radius: 10, borderColor: UIColor.clearColor(), borderWidth: 0)
cell.backgroundGray.roundCorners([.BottomLeft, .BottomRight], radius: 7, borderColor: UIColor.clearColor(), borderWidth: 0)
switch (indexPath.row) {
case 0:
cell.restImage.hnk_setImageFromURL(NSURL(string: "https://pp.vk.me/c636117/v636117560/29385/OukzPhoe4q0.jpg")!)
case 1:
cell.restImage.hnk_setImageFromURL(NSURL(string: "https://pp.vk.me/c636117/v636117560/29385/OukzPhoe4q0.jpg")!)
case 2:
cell.restImage.hnk_setImageFromURL(NSURL(string: "https://pp.vk.me/c636117/v636117560/29385/OukzPhoe4q0.jpg")!)
case 3:
cell.restImage.hnk_setImageFromURL(NSURL(string: "https://pp.vk.me/c636117/v636117560/29385/OukzPhoe4q0.jpg")!)
cell.restImage.hnk_setImageFromURL(NSURL(string: "https://pp.vk.me/c636117/v636117560/29385/OukzPhoe4q0.jpg")!)
return cell


So in the end I just added this function in dispatch_async like this and it works

self.content.layer.borderWidth = 1
self.content.layer.borderColor = UIColor(red: 235/255, green: 235/255, blue: 235/255, alpha: 1).CGColor
self.restarauntImage.roundCorners([.TopLeft, .TopRight], radius: 6, borderColor: UIColor.clearColor(), borderWidth: 0)
self.content.roundCorners([.BottomLeft, .BottomRight], radius: 6,  borderColor: UIColor(red: 235/255, green: 235/255, blue: 235/255, alpha: 1),  borderWidth: 1)})


what's more if you change number of views in your cell dynamically (for example the bottom one can be hidden), you have to add these lines of code in your cellForIndexPath to make reusable cells draw borders correctly

    cell.bottomDataView.hidden = false //show elements you have hidden
    cell.backgroundGray.layer.mask = nil //delete previous mask
    if cell.bottomDataView.layer.sublayers?.count > 2 {
        cell.bottomDataView.layer.sublayers?.removeLast() //delete previous layer