tableViewCell.roundCorners未立即显示

时间:2016-08-02 12:42:31

标签: ios swift tableview cell

我有这些方法用于collectionViewCell

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

和tableViewCell

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

问题在于它与collectionView完美配合,但在tableView中它不能立即使用.TopRight,它只适用于我多次重复使用单元格。但是.TopLeft可以工作。此外,如果我删除.TopLeft并尝试仅应用于.TopRight它也不起作用。可能是什么问题?

更新:在堆栈溢出时找到扩展名

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
    layer.addSublayer(borderLayer)
}
}

update2:cellForRowAtIndexPath,我试过在ink_setImage之后将这个方法放到每个case中,但它也没有用。

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")!)
        default:
            cell.restImage.hnk_setImageFromURL(NSURL(string: "https://pp.vk.me/c636117/v636117560/29385/OukzPhoe4q0.jpg")!)
        return cell
}
}

3 个答案:

答案 0 :(得分:0)

尝试在self.tableView.reloadData()方法的末尾发出viewDidLoad来电,这可能会解决延迟问题。

答案 1 :(得分:0)

您的实现似乎太复杂了,无法将角半径设置为imageView。 你所要做的就是:

imageView.layer.cornerRadius = <the radius>

有时,您可能需要申请:

imageView.clipToBounds = true

答案 2 :(得分:0)

所以最后我在dispatch_async中添加了这个函数,就像这样,它可以正常工作

dispatch_async(dispatch_get_main_queue(),{
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)})

}

如果您动态更改单元格中的视图数量(例如,可以隐藏底部视图),则必须在cellForIndexPath中添加这些代码行以使可重用单元格正确绘制边框

    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
    }