2016-08-02 19 views
2

Ich habe für collectionViewCelltableViewCell.roundCorners nicht sofort

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

und tableViewCell diese Methoden gezeigt

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

das Problem ist, dass es perfekt mit Collection funktioniert, aber in Tableview ist es nicht sofort funktioniert mit .TopRight, gilt es nur, wenn ich Zelle mehrmals wiederverwende. TopLeft funktioniert. Auch wenn ich .TopLeft entferne und versuche, nur auf .TopRight anzuwenden, funktioniert es auch nicht. Was könnte das Problem sein?

Update: Erweiterung gefunden auf Stack-Überlauf

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, habe ich versucht, nach ink_setImage dieser Methode auf jeden Fall zu bringen, aber es hat nicht funktioniert.

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

Wie sieht Ihre roundCorners-Methode aus? Dies ist keine Methode von UIKit ... – Arclite

+0

hinzugefügt Erweiterungscode zum Thema – JuicyFruit

+0

zeigen Sie uns cellforrowatindexpath Methode bitte –

Antwort

0

So am Ende habe ich diese Funktion in dispatch_async wie dies gerade hinzugefügt und es funktioniert

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

}

was ist mehr, wenn Sie die Anzahl der Ansichten in Ihrer Zelle dynamisch ändern (zum Beispiel die untere kann versteckt werden), müssen Sie diese Codezeilen in Ihrem CellForIndexPath hinzufügen, damit wiederverwendbare Zellen Ränder korrekt zeichnen

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

Versuchen Sie, einen self.tableView.reloadData() Anruf am Ende Ihrer viewDidLoad Methode setzen, könnte dies die Verzögerung Problem beheben.

+0

nein, habe nicht geholfen – JuicyFruit

0

Ihre Implementierung scheint zu kompliziert, um nur einen Eckenradius für ein imageView festzulegen. alles, was Sie tun müssen, ist:

imageView.layer.cornerRadius = <the radius> 

Manchmal können Sie anwenden müssen:

imageView.clipToBounds = true 
+0

Ich muss diesen Radius nur für die oberen 2 Ecken, nicht für alle 4 – JuicyFruit

+0

Ich habs. hast du versucht clipToBounds = true? es kann helfen – Lirik

+0

yeap, ich habe sowohl mein Bild und meine Sicht, hat nicht geholfen. – JuicyFruit

Verwandte Themen