在UITableViewCell内单击按钮不起作用

时间:2019-06-25 23:28:38

标签: swift uitableview appdelegate

即使我的UITableViewCell使用了Delegates,我也试图绑定一个按钮,但从未调用过该事件。

我在这里按照以下步骤操作:https://stackoverflow.com/a/40806707/7746248

我已经对所有内容进行了交叉检查,也许我错过了一些东西,而且似乎没有留下任何石头。

我没有解释为什么这不起作用!

这是我的下面的代码:

protocol OptionButtonsDelegate: class {
    func closeFriendsTapped(at index:IndexPath)
}
class MyCell: UITableViewCell {
    weak var delegate:OptionButtonsDelegate?
    var indexPath:IndexPath!
    @IBAction func closeFriendsAction(_ sender: UIButton) {
        self.delegate?.closeFriendsTapped(at: indexPath)
    }
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, OptionButtonsDelegate {

var tableArray = [MyData] ()
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
        super.viewDidLoad()

// set data & reload
self.tableView.reloadData()
}

func closeFriendsTapped(at index: IndexPath) {
    print("button tapped at index:\(index)")
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tableArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath)
            as! MyCell
        cell.delegate = self
        cell.indexPath = indexPath

        return cell
    }
}

0 个答案:

没有答案