简体   繁体   中英

Button Action from UITableViewCell to ViewController

I am trying to implement the Delegate way to do some action on a button tap from my UITableViewCell to a ViewController. The following is what I have so far -

TableViewCell:

protocol UserCellDelegate : class {
    func disableUser(cell: UserCell, button: UIButton)
}

class UserCell : UITableViewCell {

    @IBOutlet weak var userLabel: UILabel!
    @IBOutlet weak var userDescription: UILabel!

    @IBOutlet weak var writeOutUser: UIButton!

    weak var delegate: UserCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        writeOutUser.layer.cornerRadius = 5.0
    }


    @IBAction func disableUserButtonAction(sender: UIButton) {
        delegate?.disableUser(self, button: writeOutUser)  //self here I believe to be the cell 
    }


}

ViewController:

class UserDetailTableViewController : UIViewController, UserCellDelegate {
    override func viewDidLoad() {
      super.viewDidLoad()
      let userCell = UserCell()
      userCell.delegate = self
    }

    func disableUser(cell: UserCell, button: UIButton) {
      print("VC: User Disabled")
    }
}

The problem here is, the disableUser function in my ViewController never gets called. What am I doing wrong?

What is the best way to do it?

I had referred to the SO Approved Answer Here which is what I am following the same, but with no luck. Any help will be appreciated. Thanks!!

You should set the delegate of your cell in the cellForRowAtIndexPath method.As @vadian said,In the viewDidLoad you should not initialize your cell using default initializer, and the delegate is nil for your cell by this way in viewDidLoad .

Either initialise your cell using the init(style: reuseIdentifier) or set the delegate in the cell for row method as you will initialize the cell in that method anyway.

When you're doing this:

let userCell = UserCell()
userCell.delegate = self

You are setting the delegate of userCell , and only the delegate of userCell . I'm sure that the table view cells displayed in your table view is not userCell .

So instead of setting the delegate of userCell , you should set the delegate of the cells that you actually want to show in the table view. The most probable place to create new cells that you want to show in table view is in the tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) method.

In the method, you must have something like this:

let cell = ...
// setting properties of cell using the model
return cell

You just need to add this line before the return:

cell.delegate = self

In your code UserCell acts like a UIView . You forgot adding userCell to current controller's view. If you want to use UITableViewCell , the better way is using it with UITableView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM