Pass multiple parameters to addTarget

前端 未结 5 1954
小鲜肉
小鲜肉 2020-12-05 08:46

In my UITableViewCell I have a button. And I want to add action to it by passing multiple parameters in cellForRowAtIndexPath method.



        
5条回答
  •  情书的邮戳
    2020-12-05 08:53

    Easy!

    1: Subclass UIButton:

    class IndexPathCellButton: UIButton {
        var indexPath:NSIndexPath?
    }
    

    2: Set your value:

    ...
    cell.indexPathButton.addTarget(self, action: #selector(LocationSearchViewController.cellOptionButtonClick(_:)), forControlEvents: .TouchUpInside)
    cell.indexPathButton.indexPath = indexPath // Your value
    ...
    

    3: Retrieve your value:

    @objc private func cellOptionButtonClick(sender: IndexPathCellButton) {
        print(sender.indexPath)
    }
    

提交回复
热议问题