我有一个带有按钮的自定义单元格的表格视图..如果我点击按钮,则应该执行操作..
问题是按钮没有被按下,整个单元格被点击,我想要的动作永远不会发生!
如何解决?我想点击按钮而不是单元格,并在点击时执行特定操作..
这里我正在做什么,但没有工作..
自定义单元格:
class CustomViewCell8: UITableViewCell {
var onButtonTapped : (() -> Void)? = nil
@IBOutlet weak var servicebtn: UIButton!
@IBAction func Clicked(_ sender: UIButton) {
if let onButtonTapped = self.onButtonTapped{
onButtonTapped()
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
并在视图控制器中:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "CustomCell8") as! CustomViewCell8
cell.servicebtn.setBackgroundImage(UIImage(named: serviceimage[indexPath.row]) as UIImage?, for: UIControlState.normal)
cell.onButtonTapped = {
if self.number == 1 {
if indexPath.row == 0{
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: " ") as UIViewController
self.present(vc, animated: true, completion: nil)
let userDefaults = UserDefaults.standard
let decoded = userDefaults.object(forKey: "services") as! Data
let decodedService = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Service]
for service in decodedService {
if service.name == " "{
GetShiftsAndPrices(id: service.id)
}
}
}else if indexPath.row == 1{
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: " ") as UIViewController
self.present(vc, animated: true, completion: nil)
let userDefaults = UserDefaults.standard
let decoded = userDefaults.object(forKey: "services") as! Data
let decodedService = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Service]
for service in decodedService {
if service.name == " "{
GetShiftsAndPrices(id: service.id)
}
}
}else if indexPath.row == 2{
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: " ") as UIViewController
self.present(vc, animated: true, completion: nil)
let userDefaults = UserDefaults.standard
let decoded = userDefaults.object(forKey: "services") as! Data
let decodedService = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Service]
for service in decodedService {
if service.name == " "{
GetShiftsAndPrices(id: service.id)
}
}
}
......
return cell
}
这之前和我一起工作..但现在不行!我不知道出了什么问题。我做错了吗?
答案 0 :(得分:1)
您表示即使您的print("here")
按钮中的@IBAction
也无法打印。您还表示您的@IBAction
已正确连线。这意味着您的原型单元存在问题。如果包含按钮的视图小于按钮,则您无法单击它。检查原型单元的布局。
原始答案
看起来它应该有效。
三条评论:
您应该在关闭声明中添加[unowned self]
以避免强引用循环,这将阻止UITableView
被释放。由于该单元格不会比其包含的UITableView
更长,因此无需对self
进行强有力的引用。
cell.onButtonTapped = { [unowned self]
if self.number == 1 {
...
您可以使用可选链接替换:
if let onButtonTapped = self.onButtonTapped{
onButtonTapped()
}
简单地说:
self.onButtonTapped?()
您的代码非常重复。如果行之间的唯一区别是下一个ViewController的标识符,那么你可以这样做:
let nextvcid = ["foo", "bar", "baz"][indexPath.row]
cell.onButtonTapped = { [unowned self]
if self.number == 1 {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: nextvcid)
self.present(vc, animated: true, completion: nil)
let userDefaults = UserDefaults.standard
let decoded = userDefaults.object(forKey: "services") as! Data
let decodedService = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Service]
for service in decodedService {
if service.name == " "{
GetShiftsAndPrices(id: service.id)
}
}