2016-08-02 80 views
2

顯示我的這些方法collectionViewCelltableViewCell.roundCorners不會立即

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之後,但它也不能工作。

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 
} 
} 
+0

什麼是您的roundCorners方法是什麼樣子?這不是由UIKit提供的方法... – Arclite

+0

添加擴展代碼主題 – JuicyFruit

+0

我們展示的cellForRowAtIndexPath方法請 –

回答

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 
    } 
0

嘗試把一個self.tableView.reloadData()呼叫你viewDidLoad方法結束,這可能會解決延遲問題。

+0

不,沒有幫助 – JuicyFruit

0

你的實現似乎太複雜了,只是將一個角半徑設置爲imageView。 所有你需要做的是:

imageView.layer.cornerRadius = <the radius> 

有時候,你可能需要申請:

imageView.clipToBounds = true 
+0

我只需要對頂部邊角2設置此半徑,不是所有的4 – JuicyFruit

+0

得到它了。你試過clipToBounds = true嗎?它可能會幫助 – Lirik

+0

yeap,我做了我的形象和我的看法,沒有幫助。 – JuicyFruit