由于开发和PGDev,我能拿出一个很好的解决方案,为static cells
运作良好。我为UITableViewCell
制作了extension
,看起来像这样。
let separatorColour = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 0.5)
extension UITableViewCell {
func separator(topInset: CGFloat, btmInset: CGFloat, showTop: Bool, showBtm: Bool) {
if showTop {
let topSeparator: UIView = UIView()
topSeparator.frame = CGRect(x: topInset, y: 0, width: frame.width, height: 0.5)
topSeparator.backgroundColor = separatorColour
addSubview(topSeparator)
}
if showBtm {
let btmSeparator: UIView = UIView()
btmSeparator.frame = CGRect(x: btmInset, y: frame.height - 0.5, width: frame.width, height: 0.5)
btmSeparator.backgroundColor = separatorColour
addSubview(btmSeparator)
}
}
func btnSeparator() {
let topSeparator: UIView = UIView()
topSeparator.frame = CGRect(x: 0, y: 0, width: frame.width, height: 0.5)
topSeparator.backgroundColor = separatorColour
addSubview(topSeparator)
let btmSeparator: UIView = UIView()
btmSeparator.frame = CGRect(x: 16, y: frame.height - 0.5, width: frame.width - 32, height: 0.5)
btmSeparator.backgroundColor = separatorColour
addSubview(btmSeparator)
}
}
在第一个功能separator()
你可以简单地选择顶部和底部separator
以及它们是否可见或不可见的插图。
TOPCELL 所以在cell
与name label
在图片如上所示,顶部和底部separators
的情况下是可见的。但是,因为它应该在该部分中显示为第一个cell
,所以它没有顶部插入和适当的底部插入。
MiddleCell 在cell
与email label
它只有的情况下separator
(带有插图)可见与上述cell
具有底部separator
。
BottomCell 对于cell
与password label
,它只具有较低separator
(无偏移)可见与上述cell
具有底部separator
。
SpecialCase 的btnSeparator()
功能只是为了帮助和支持按钮,在这里我想对双方插页的情况。
我们实现这个extension
,我用了一个switch statement
的cellForRowAt
函数内部给每个cell
适当extension
,这是看起来像什么:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAt: indexPath)
let inset: CGFloat = 16
switch indexPath {
// First Section
case [0,1]:
cell.separator(topInset: 0, btmInset: inset, showTop: true, showBtm: true)
case [0,2]:
cell.separator(topInset: inset, btmInset: inset, showTop: false, showBtm: true)
case [0,3]:
cell.separator(topInset: inset, btmInset: 0, showTop: false, showBtm: true)
// Second Section
case [1,0]:
cell.separator(topInset: 0, btmInset: inset, showTop: true, showBtm: true)
case [1,1]:
cell.separator(topInset: inset, btmInset: inset, showTop: false, showBtm: true)
case [1,2]:
cell.separator(topInset: inset, btmInset: 0, showTop: false, showBtm: true)
// Third Section
case [2,0]:
cell.btnSeparator()
case [2,1]:
cell.separator(topInset: inset, btmInset: 0, showTop: false, showBtm: true)
case [2,3]:
cell.separator(topInset: 0, btmInset: 0, showTop: true, showBtm: true)
default:
print("default")
}
return cell
}
一定要设置separator
首屈一指的Storyboard
或使用:
tableView.separatorStyle = UITableViewCellStyle.None
我追加'UIView'到自定义单元格,将它分离器,隐藏DEFA ult分隔符,所以很容易隐藏/显示/颜色/不管 – JuicyFruit