将目标添加到 UITableViewCell 中的 UIButton
Add target to UIButton inside UITableViewCell
我的 CustomTableViewCell
里面有 UIButton
我也调用 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
中声明):
@objc func eyeButtonTapped(){
print("Hi")
}
当我点击 button
但没有任何反应时出现 "tap-animation"..
有人知道为什么会这样吗?我该如何适应它?
声明您的按钮时,self
此时不存在。
在您的设置函数中添加目标,或将您的声明更改为 lazy var
:
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 动作:@selector(updateDate:) forControlEvents:UIControlEventTouchUpInside];
我的 CustomTableViewCell
里面有 UIButton
我也调用 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
中声明):
@objc func eyeButtonTapped(){
print("Hi")
}
当我点击 button
但没有任何反应时出现 "tap-animation"..
有人知道为什么会这样吗?我该如何适应它?
声明您的按钮时,self
此时不存在。
在您的设置函数中添加目标,或将您的声明更改为 lazy var
:
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 动作:@selector(updateDate:) forControlEvents:UIControlEventTouchUpInside];