UITableViewCell重新排序控件 - 设置背景颜色

5
我需要在我的表格视图中使用自定义的表格视图单元,其中部分背景是白色,另一部分是灰色。这一切都很完美 - 直到重新排序出现:

enter image description here

我的问题是重新排序控件全部是灰色的,但我希望它部分是白色的,基本上看起来像表格的一部分。我能够使用以下代码进入视图:
        for view in cell.subviews {
            if String(describing: view.self).contains("UITableViewCellReorderControl") {
                view.backgroundColor = .white
            }
        }

但是:在这里将视图的背景颜色设置为白色会看起来像这样:

enter image description here

显然我不想要这个,我希望灰色能一直延伸到右侧。 我尝试了各种修改视图的方法(例如将框架高度设置得更小,使用CGTransform等),但似乎都没有任何影响!?

我真的很感激任何解决方法! 谢谢!

3个回答

0
1. 将灰色线条放在节头视图上; 2. 将单元格的背景颜色设置为白色;
YourCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .white
    }
}

0
最简单的方法是使用单元格的backgroundView。
class CustomCell : UITableViewCell {
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.setupAtInit()
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setupAtInit()
    }
    func setupAtInit() {
        self.backgroundVisew = UIView() //<- Here
        self.backgroundView?.backgroundColor = UIColor.white //The color you want to.
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        // You can layout backgroundView as you want.
        // Basically, the backgroundView bounds is the same as self.bounds.
        // self.backgroundView?.frame == self.bounds
    }
}

0

这段代码应该可以在UITableViewCell中运行。它会激活其editMode,查找、隐藏子视图并重置整个单元格上的Reorder Control。关键是要使用layoutSubviews()方法。

weak var reorderControl: UIView?
override public func awakeFromNib() {
    super.awakeFromNib()
    setEditing(true, animated: false)
    reorderControl = findTheReorderControl()
    removeSubviews(from: reorderControl)
}
private func findTheReorderControl() -> UIView? {
    return subviews.first { view -> Bool in
        let className = String(describing: type(of:view))
        return className == "UITableViewCellReorderControl"
    }
}
private func removeSubviews(from reorderControl: UIView?) {
    reorderControl?.subviews.forEach({$0.removeFromSuperview()})
}
override var showsReorderControl: Bool {
    get {
        return true // short-circuit to on
    }
    set {}
}
override func setEditing(_ editing: Bool, animated: Bool) {
    if editing == false {
        return // ignore any attempts to turn it off
    }
    super.setEditing(editing, animated: animated)
}
override func layoutSubviews() {
    super.layoutSubviews()
    reorderControl?.frame = bounds
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接