UITableViewCell 去掉选中效果且能够选中复选框

2019-11-21  本文已影响0人  烟影很美

去掉选中效果如下:

override func awakeFromNib() {
        self.selectionStyle = .none
}

这种方式大家一定很熟悉, 但是采用这种方式去掉选中效果后, cell编辑状态下的选择框就不能选中了, 可以做如下处理:

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        if self.isEditing == true {
            if let view = self.findView(view: self, cla: NSClassFromString("UITableViewCellEditControl")!) {
                let control: UIControl = view as! UIControl
                
                control.isSelected = selected
            }
        }
    }
    
    func findView(view: UIView, cla: AnyClass) -> UIView? {
        if view.subviews.count == 0 {
            return nil
        }
        
        for subView in view.subviews {
            if subView.isKind(of: cla) {
                return subView
            } else {
                if let v = self.findView(view: subView, cla: cla) {
                    if v.isKind(of: cla) {
                        return v
                    }
                }
            }
        }
        return nil
    }
上一篇下一篇

猜你喜欢

热点阅读