Ios 将目标添加到UITableViewCell内的UIButton
我的Ios 将目标添加到UITableViewCell内的UIButton,ios,swift,uitableview,uibutton,Ios,Swift,Uitableview,Uibutton,我的CustomTableViewCell中有ui按钮,我还调用addTarget: let eyeButton: UIButton = { let v = UIButton() v.addTarget(self, action: #selector(eyeButtonTapped), for: .touchUpInside) v.setImage(UIImage(named: "eyeOpen"), for: .normal) v.translatesAutor
CustomTableViewCell
中有ui按钮
,我还调用addTarget
:
let eyeButton: UIButton = {
let v = UIButton()
v.addTarget(self, action: #selector(eyeButtonTapped), for: .touchUpInside)
v.setImage(UIImage(named: "eyeOpen"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
但是,未调用此函数(也在CustomTableViewCell
中声明):
当我点击按钮时会出现“点击动画”,但什么也没发生
有人知道为什么会发生这种情况以及我如何适应它吗?当声明您的按钮时,self
此时不存在
在setup func中添加目标,或将声明更改为惰性变量
:
lazy var eyeButton: UIButton = {
let v = UIButton()
v.addTarget(self, action: #selector(eyeButtonTapped), for: .touchUpInside)
v.setImage(UIImage(named: "eyeOpen"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
创建按钮出口名称cellbuttonName或任何其他名称,而不是在tableviewdelegate cellForRowAtIndexPath中编写该代码
[self.cellbuttonName addTarget:self action:@selector(updateDate:)for controlEvents:UIControlEventTouchUpInside]
lazy var eyeButton: UIButton = {
let v = UIButton()
v.addTarget(self, action: #selector(eyeButtonTapped), for: .touchUpInside)
v.setImage(UIImage(named: "eyeOpen"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()