重写drawRect

时间:2015-08-04 10:33:27

标签: ios swift uitableview

到目前为止,为了在录制时突出显示我的单元格的错觉,它只是实现了tableView:didSelectRowAtIndexPath:这样:

tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: false)
}

现在我通过覆盖drawRect来自定义我的单元格的contentView(实际上是contentView的子视图)的渲染。问题是我的伎俩不再“耍手段”了。

所以我考虑像这样实施tableView:didHighlightRowAtIndexPath:tableView:didUnhighlightRowAtIndexPath:

override func drawRect(rect: CGRect) {
    let context: CGContextRef = UIGraphicsGetCurrentContext()
    let colors: [CGColor]!
    if isHighlighted {
        colors = [UIColor.blackColor().CGColor, UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1).CGColor]
    } else {
        colors = [UIColor.whiteColor().CGColor, UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1).CGColor]
    }

    let rect = self.bounds

    GraphicHelper.drawLinearGradient(context, rect: rect, colors: colors)

    let strokeRect = CGRectInset(rect, 1, 1)
    CGContextSetStrokeColorWithColor(context, UIColor.whiteColor().CGColor)
    CGContextSetLineWidth(context, 1)
    CGContextStrokeRect(context, strokeRect)

    let separatorColor = UIColor(red: 208/255, green: 208/255, blue: 208/255, alpha: 1).CGColor
    let startPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height - 1)
    let endPoint = CGPointMake(rect.origin.x + rect.size.width - 1, rect.origin.y + rect.size.height - 1)
    GraphicHelper.drawStroke(context, startPoint: startPoint, endPoint: endPoint, color: separatorColor, width: 1)
}

它可以解决这个问题,但仅限于“长”水龙头,因为它不起作用或短暂的水龙头是完全没用的。

关于如何实现这一目标的任何建议?

1 个答案:

答案 0 :(得分:0)

我的愚蠢想法

  1. UIImageView放入自定义单元格内容视图中以用于后台目的。 (设置白色或透明图像)

  2. didSelectRowAtIndexPath中更改UIImageView的图片并​​重置上次选择的单元格图片。

  3. 你也可以改变背景颜色。