Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableViewCell drawRect底部边框在insertRow上消失_Ios_Swift_Uitableview_Swift2 - Fatal编程技术网

Ios UITableViewCell drawRect底部边框在insertRow上消失

Ios UITableViewCell drawRect底部边框在insertRow上消失,ios,swift,uitableview,swift2,Ios,Swift,Uitableview,Swift2,我有一个自定义UITableViewCell,在其中我使用drawRect绘制简单的底部边框: override func drawRect(rect: CGRect) { let context = UIGraphicsGetCurrentContext() CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect)) CGContextAddLineToPoint(context, CG

我有一个自定义UITableViewCell,在其中我使用drawRect绘制简单的底部边框:

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)

你知道如何避免这种情况吗?

这不是直接的答案,只是我的建议,如何在不画画的情况下获得相同的视觉效果

请参见示例代码和结果图像

@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)
}
IB中配置的按钮和背景视图颜色


如果您只想使用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)
}
您将得到如下输出:

另外,将tableview分隔符颜色设置为透明

self.tableView.separatorColor = UIColor.clearColor()
这样它就不会和你的图层重叠。
我所能观察到的是,无论何时插入新行,细胞的边界都不会消失

或者
我们可以在单元故事板中使用UIImageView,并提供具有以下约束的颜色。
添加UIImageView

向UIImageView添加约束

我们完了


还有另外一个备选解决方案可以使用贝塞尔路径来实现这一点

 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)
 }
这也会产生相同的输出。

编辑:

如果我们没有为单元使用故事板或NIB并以编程方式创建单元,那么我们可以这样做:

在CustomTableViewCell类中创建属性

var borderLayer:CALayer!
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)
}
现在在CustomTableViewCell类中有一个名为layoutSubviews的方法

var borderLayer:CALayer!
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)
}

试试看。

我认为,添加子层(背景色和高度的CALayer)可以获得相同的视觉效果,并且完全避免绘制。但这当然不是你问题的答案:)@sage444如果这是一个可行的替代方案,请添加它作为答案。我们可以从故事板本身在单元格底部有一个2像素的简单uiimageview。并在那里提供一种颜色…用作边框。您也在呼叫手机设置需要显示。。为了再次调用drawRect方法,我看到了问题所在。我无法将其放入UITableViewCell类中,因为它没有viewDidLoad。当我把它放到类的init函数中时,高度和宽度还没有通过autolayout计算出来。哦,我使用这个方法只是举个例子,你可以在awakeFromNib上或在你的自定义setData方法中进行计算为什么要使事情复杂化。。只需在情节提要单元本身中添加一个2像素的简单UIView或uiimage视图,其高度固定为2像素,底部约束为0,左右约束为0。。就是这样。@RajanMaheshwari您部分正确,但UIView与CALayer不太一样,在tableViewCell中可以添加overhead@sage444这就是为什么我写了我们可以使用UIImageView而不是UIView。。这将是非常少的开销这也是一个很好的答案,但为什么你不添加UIImageView替代方案,而不是使用我的想法?看,我也使用CALayers和BezierPath。对于UIImageView部件,它比添加图层简单得多,也不太复杂。此外,UIImageView的开销非常小。我也会在答案中更新备选方案。期待看到Bezierpath,您的答案似乎是关于分隔符的,但关于按钮边框的问题您确定是关于按钮的吗?它是关于带有底部边框的tablecell