2015-11-17 21 views
3

我有一個自定義的UITableViewCell,我使用的drawRect畫一個簡單的底邊框:的UITableViewCell的drawRect底邊框上消失的insertRow

override func drawRect(rect: CGRect) { 
    let context = UIGraphicsGetCurrentContext() 
    CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect)) 
    CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect)) 
    CGContextSetStrokeColorWithColor(context, UIColor(netHex: 0xEFEEF4).CGColor) 
    CGContextSetLineWidth(context, 2) 
    CGContextStrokePath(context) 
} 

這完美的作品。然而,當我插入帶有動畫中的所有單元格的邊框消失一行,並再次出現時插入動畫完成:

tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: cells.count - 1, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Fade) 

任何想法如何避免這種情況?

+0

我認爲你可以通過額外的子圖層(CALayer具有背景顏色和高度)獲得相同的視覺效果,並且完全避免繪圖。但它當然不是你的問題的答案:) – sage444

+0

@ sage444如果它是一個工作的替代品,請添加它作爲答案。 –

+0

我們可以在故事板本身的單元格底部有一個2像素的簡單uiimageview,並在那裏提供一個顏色...這將作爲邊框。你還需要調用單元格setNeedsDisplay ..以便再次調用drawRect方法嗎? –

回答

6

那麼,如果你只想使用CALayers假設你的TableView是覆蓋設備的整個寬度你可以嘗試的TableCell的awakeFromNib此代碼。

override func awakeFromNib() { 
    super.awakeFromNib() 
    let borderLayer = CALayer() 
    let lineHeight:CGFloat = 2 
    borderLayer.frame = CGRect(x: 0, y: self.frame.height - lineHeight , width: UIScreen.mainScreen().bounds.width, height: lineHeight) 
    borderLayer.backgroundColor = UIColor.redColor().CGColor 
    self.layer.addSublayer(borderLayer) 
} 

,你會得到的輸出爲: enter image description here

也會使你的tableview分離器顏色以清除

self.tableView.separatorColor = UIColor.clearColor() 

所以它不會得到你的層重疊。
我可以觀察到,現在每當插入新行時,單元格的邊界就不會消失。

或者
我們可以只使用一個UIImageView在小區故事板,並提供一個顏色有以下限制。
添加的UIImageView
enter image description here

添加約束到的UIImageView

enter image description here

就搞定!


還有一個備用溶液來實現這一點使用貝塞爾路徑

override func awakeFromNib() { 
    super.awakeFromNib() 
    let line = CAShapeLayer() 
    let linePath = UIBezierPath() 
    linePath.moveToPoint(CGPointMake(0, self.frame.height)) 
    linePath.addLineToPoint(CGPointMake(UIScreen.mainScreen().bounds.width, self.frame.height)) 
    line.lineWidth = 3.0 
    line.path = linePath.CGPath 
    line.strokeColor = UIColor.redColor().CGColor 
    self.layer.addSublayer(line) 
} 

這也產生相同的輸出。

編輯:

如果我們不使用的電池故事板或筆尖和編程創建單元格,然後我們可以做一個變通方法是這樣的:

在CustomTableViewCell類創建一個屬性

var borderLayer:CALayer! 

在CustomTableViewCell類現在有一個叫方法layoutSubviews

override func layoutSubviews() { 
    if(borderLayer != nil) { 
     borderLayer.removeFromSuperlayer() 
    } 
    borderLayer = CALayer() 
    let lineHeight:CGFloat = 2 
    borderLayer.frame = CGRect(x: 0, y: self.frame.height - lineHeight , width: UIScreen.mainScreen().bounds.width, height: lineHeight) 
    borderLayer.backgroundColor = UIColor.redColor().CGColor 
    self.layer.addSublayer(borderLayer) 
} 

試試看。

+0

這也是一個很好的答案,但爲什麼你不添加UIImageView的替代而不是使用我的想法? – sage444

+0

請參閱我也使用CALayers和BezierPaths。對於UIImageView部件來說,它比添加圖層更簡單並且更簡單。另外UIImageView的開銷很小。我會在答案中更新備選方案。 –

+0

期待看到BezierPath – sage444

1

這不是直接的答案,只是我的建議如何在沒有繪圖的情況下達到相同的視覺效果。

參見示例代碼和結果圖像中IB配置

@IBOutlet weak var button: UIButton! 
override func viewDidLoad() { 
    super.viewDidLoad() 

    let layer = CALayer() 
    let lineHeight:CGFloat = 2 
    layer.frame = CGRect(x: 0, y: button.bounds.height - lineHeight , width: button.bounds.width, height: lineHeight) 
    layer.backgroundColor = UIColor(colorLiteralRed: 0xEF/255.0, green: 0xEE/255.0, blue: 0xF4/255.0, alpha: 1).CGColor 
    button.layer.addSublayer(layer) 
} 

按鈕和背景圖的顏色。

enter image description here

+0

我看到問題了。我不能把它放到我的UITableViewCell類中,因爲它沒有viewDidLoad。當我將它放入類的init函數中時,高度和寬度尚未通過自動佈局進行計算。 –

+0

呵呵,我只是用這個方法,例如,你可以在awakeFromNib或你自定義的setData方法中做到這一點 – sage444

+0

爲什麼複雜的事情..只需添加一個簡單的UIView或2像素的故事板單元格本身的視圖與它的高度2像素固定和底部約束爲0,左,右爲0 ..完蛋了.. –