2017-06-24 149 views
0

我一直沒有成功將我的自定義UIView添加到我的TableViewCell。此自定義視圖表示每個表視圖單元格的自定義複選標記。我正在做我的項目沒有故事板,我不知道如何以編程方式將此自定義UIView添加到我的UITableViewCell。這裏是我的代碼:將自定義UIView添加到TableViewCell

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走錯了。

+0

哪裏是你的'cellForRow'方法? –

+0

方明寧它在我的桌子,但我沒有發佈它,因爲它是很多代碼 – ajayb

+0

好的。所以你的圖像視圖被正確添加,但不是你的自定義視圖,對吧? –

回答

0

你應該實例化你的CheckBoxView,而不是此行蒙上了正常UIView您CheckBoxView

變化:

let view = UIView() as! CheckBoxView 

這一行

let view = CheckBoxView() 
+0

它要求讓view = CheckBoxView(編碼器:<#NSCoder#>),但我不確定要在編碼器中放置什麼。我使用了一個教程來設置這個,所以我不是100%熟悉代碼的工作方式 – ajayb

+0

然後你在沒有編碼器的情況下添加一個'init'並且你的'CheckBoxView' – muescha

+0

我對此有點不熟悉。你能舉出一個代碼的例子嗎?我真的很感激 – ajayb