如何为UITableViewCell的.selectedBackgroundView设置自定义边距

时间:2015-07-09 16:01:44

标签: ios swift uitableview

我正在使用表视图来显示树结构。每个单元格对应于用户可以展开或折叠的节点。通过在单元的前沿处具有越来越大的凹痕来可视化每个节点的水平。这些缩进是使用 layoutMargins 设置的。这似乎适用于细胞的标签和分离器。这是一些代码:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    let cellLevel = cellLevelForIndexPath(indexPath)
    let insets: UIEdgeInsets = UIEdgeInsetsMake(0.0, CGFloat(cellLevel) * 20.0, 0.0, 0.0)

    cell.separatorInset = insets
    cell.layoutMargins = insets
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("cellId") as? UITableViewCell
    if  cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cellId")
    }

    cell!.preservesSuperviewLayoutMargins = false
    cell!.backgroundColor = UIColor.clearColor()

    let cellLevel = cellLevelForIndexPath(indexPath)
    if let textlabel = cell!.textLabel {
        textlabel.text = "Cell @ level \(cellLevel)"
        textlabel.textColor =  UIColor.blackColor()
    }

    cell!.selectedBackgroundView = UIView(frame: CGRectZero)
    cell!.selectedBackgroundView.backgroundColor = UIColor.cyanColor()

    return cell!
}

结果表如下所示:

Not what I want :(

我现在面临的问题是:如何优雅地将相同的缩进应用于单元格的.selectedBackgroundView,以使其与文本和分隔符行齐平?最终结果应如下所示:

What I want :)

注意:我目前正在通过使.selectedBackgroundView更复杂并添加不同大小的背景色子视图来实现所需效果,这些子视图有效地屏蔽了单元格的某些部分,例如:像这样:

let maskView = UIView(frame: CGRectMake(0.0, 0.0, CGFloat(cellLevel) * 20.0, cell!.bounds.height))
maskView.backgroundColor = tableView.backgroundColor
cell!.selectedBackgroundView.addSubview(maskView)

但我强烈认为必须有更好的方法来做到这一点。

2 个答案:

答案 0 :(得分:0)

想出一种让它发挥作用的方法。我的诀窍是停止将 .selectedBackgroundView 视为可见高亮本身(因此试图掩盖或调整它)并将其视为更像画布。

这是我最终做的事情。首先是为每个级别获取适当插入的更方便的方法:

let tableLevelBaseInset = UIEdgeInsetsMake(0.0, 20.0, 0.0, 0.0)

private func cellIndentForLevel(cellLevel: Int) -> CGFloat {
       return CGFloat(cellLevel) * tableLevelBaseInset.left
}

然后在 cellForRowAtIndexPath

cell!.selectedBackgroundView = UIView(frame: CGRectZero)
cell!.selectedBackgroundView.backgroundColor = UIColor.clearColor()

let highlight = UIView(frame: CGRectOffset(cell!.bounds, cellIndentForLevel(cellLevel), 0.0)) 
highlight.backgroundColor = UIColor.cyanColor()
cell!.selectedBackgroundView.addSubview(highlight)

似乎工作得很好。

答案 1 :(得分:0)

    var selectedView = UIView()    
    override func awakeFromNib() {
                super.awakeFromNib()
        
                
                self.selectedBackgroundView = {
                    let view = UIView()
                    view.backgroundColor = UIColor.clear
                    selectedView.translatesAutoresizingMaskIntoConstraints = false
                    selectedView.backgroundColor = .hetro_systemGray6
                    selectedView.roundAllCorners(radius: 8)
                    view.addSubview(selectedView)
                    selectedView.topAnchor.constraint(equalTo: view.topAnchor, constant: 5).isActive = true
                    selectedView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -5).isActive = true
                    selectedView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
                    selectedView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
                    return view
                }()
 }
    
override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
            
            if selected {
                selectedBackgroundView?.isHidden = false
            } else {
                selectedBackgroundView?.isHidden = true
            }
        }