【问题标题】:UITableView: customize separatorUITableView:自定义分隔符
【发布时间】:2016-08-26 13:33:30
【问题描述】:

我的问题很简单,如何在UITableView中实现这样的分隔符。

我曾经尝试通过此代码将包含虚线的 CAShapeLayer 添加到 Cell.layer

let screenWidth = UIScreen.mainScreen().bounds.width
let path = UIBezierPath(); path.lineWidth = 0.2
path.moveToPoint(CGPointMake(10, cell.height))
path.addLineToPoint(CGPointMake(screenWidth - 20, cell.height))
let shapeLayer:CAShapeLayer = CAShapeLayer()
shapeLayer.strokeColor = UIColor.blackColor().colorWithAlphaComponent(0.3).CGColor
shapeLayer.fillColor = nil
shapeLayer.lineDashPattern = [1,3]
shapeLayer.path = path.CGPath
cell.layer.addSublayer(shapeLayer)

但它不适用于自调整大小的表格视图单元格

tableView.rowHeight = UITableViewAutomaticDimension

感谢您的帮助!

【问题讨论】:

标签: swift uitableview


【解决方案1】:

您可以使用带有虚线的image 来实现此目的。试试这个:

self.tableView.separatorColor = UIColor(patternImage: UIImage(named: "YourDottedImage")!)

【讨论】:

  • 我明天试试,不如创建一个 tUITableViewCellSeparatorStyle 的子类,然后在里面做一些有趣的事情:))
  • 你确定你可以继承UITableViewCellSeparatorStyle吗?
  • 老实说,我认为这是不可能的 :)),但我们可以通过扩展或其他方式扩展 ex
【解决方案2】:

你可以尝试向上移动线

path.moveToPoint(CGPointMake(10, cell.height-1))
path.addLineToPoint(CGPointMake(screenWidth - 20, cell.height-1))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    相关资源
    最近更新 更多