accessoryButtonTappedForRowWithIndexPath: not getting called

后端 未结 8 1903
执念已碎
执念已碎 2020-12-05 13:01

I am creating a Detail disclosure button which is getting populated using an array.... However the accessoryButtonTappedForRowWithIndexPath: function is not being called in

8条回答
  •  遥遥无期
    2020-12-05 13:57

    Converting the top answer to Swift 3:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        ...
        let unseletcedImage = UIImage(named: "")
        let seletcedImage = UIImage(named: "")
        let button = UIButton(type: .custom)
        // match the button's size with the image size
        let frame = CGRect(x: CGFloat(0.0), y: CGFloat(0.0), width: CGFloat((unseletcedImage?.size.width)!), height: CGFloat((unseletcedImage?.size.height)!))
        button.frame = frame
        button.setBackgroundImage(unseletcedImage, for: .normal)
        button.setBackgroundImage(seletcedImage, for: .selected)
        cell?.accessoryView = button
        let action = #selector(checkButtonTapped(sender:event:))
        (cell?.accessoryView as? UIButton)?.addTarget(self, action: action, for: .touchUpInside)
        ....
        return cell!
    
    }
    
    @objc func checkButtonTapped(sender: UIButton?, event: UIEvent) {
        let touches = event.allTouches
        let touch = touches!.first
        guard let touchPosition = touch?.location(in: self.tableView) else {
            return
        }
        if let indexPath = tableView.indexPathForRow(at: touchPosition) {
            tableView(self.tableView, accessoryButtonTappedForRowWith: indexPath)
        }
    }
    

提交回复
热议问题