将自定义UIView添加到TableViewCell

时间:2017-06-24 01:25:02

标签: ios swift uiview tableviewcell

我没有成功将自定义UIView添加到我的TableViewCell。此自定义视图表示每个表视图单元格的自定义复选标记。我正在做没有故事板的项目,我不知道如何以编程方式将这个自定义UIView添加到我的UITableViewCell。这是我的代码:

class CheckBoxView: UIView {

var isChecked: Bool
var checkBoxImageView: UIImageView
var checkBoxChanged :() -> () = { }

required init?(coder aDecoder: NSCoder) {
    self.isChecked = false
    self.checkBoxImageView = UIImageView(image: nil)

    super.init(coder: aDecoder)

    setup()
}

func setup() {

    self.layer.borderWidth = 1.0
    self.isUserInteractionEnabled = true

    self.checkBoxImageView.frame = CGRect(x: 2, y: 2, width: 25, height: 25)
    self.addSubview(self.checkBoxImageView)

    let selector: Selector = "checkBoxTapped"

    let tapRecognizer = UITapGestureRecognizer(target: self, action: selector)
    self.addGestureRecognizer(tapRecognizer)

}

func checkBoxTapped() {
    self.checkBoxChanged()
}

func markAsChecked() {
    self.checkBoxImageView.image = UIImage(named: "new_message_icon")
}

func markAsUnChecked() {
    self.checkBoxImageView.image = nil
}

}

这是我的TableViewCell:

class AddContactsCell: UITableViewCell {

let profileImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.layer.cornerRadius = 20
    imageView.layer.masksToBounds = true
    imageView.contentMode = .scaleAspectFill
    return imageView
}()

let checkBox: CheckBoxView = {
    let view = UIView() as! CheckBoxView
    view.backgroundColor = UIColor.blue
    view.translatesAutoresizingMaskIntoConstraints = false
    view.layer.cornerRadius = 16
    view.layer.masksToBounds = true
    return view
}()

override func layoutSubviews() {
    super.layoutSubviews()

    textLabel?.frame = CGRect(x: 100, y: textLabel!.frame.origin.y - 2, width: textLabel!.frame.width, height: textLabel!.frame.height)

    detailTextLabel?.frame = CGRect(x: 101, y: detailTextLabel!.frame.origin.y + 1, width: detailTextLabel!.frame.width, height: detailTextLabel!.frame.height)
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)

    addSubview(profileImageView)
    self.addSubview(checkBox)

    //ios 9 constraint anchors
    //need x,y,width,height anchors
    profileImageView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 50).isActive = true
    profileImageView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
    profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true

    checkBox.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 10).isActive = true
    checkBox.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    checkBox.widthAnchor.constraint(equalToConstant: 30).isActive = true
    checkBox.heightAnchor.constraint(equalToConstant: 30).isActive = true
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

如果你能告诉我添加UIView的错误,我真的很感激。

1 个答案:

答案 0 :(得分:0)

你应该实现你的 has_many :units, :through => :lease 而不是将正常的CheckBoxView转换为你的CheckBoxView

更改此行:

UIView

到这一行

let view = UIView() as! CheckBoxView