I have a problem, I created some buttons in a cell of a UITableView, my cell has a class where the IBoutlets of my labels and my buttons are created, and in the cellForRowAt method I assign my cell as follows:
let cell = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PostViewCell
the problem I have is that when I want to use one of the buttons that I create from that cell I do the following:
cell.btnLike.addTarget(self, action: #selector(addLike(idPost:idLike:)), for: .touchUpInside)
I do it this way to be able to access a function called addLike that receives two parameters:
@objc func addLike(idPost: Int, idLike: Int){
print("Like")
}
As I am making use of a #selector to my function, I added the @objc so that it can be used within that selector, now my problem is that I don't know how I can pass the two parameters, that is, something similar to this:
#selector(addLike(idPost: 1, idUserPost: 2))
If I do it this way, I get an error, my question is, how can I pass two parameters from the #selector.
Parameters cannot be sent according to Apple documentation. The best way to work is with
Protocolos
. I leave you a simple example:PostViewCell.swift
TableViewController.swift
This way each button
fila
will have a unique identifier assigned to it.Good morning, I recommend you use closures, taking the example of the person above, the code would be the following.
PostViewCell.swift
TableViewController.swift
consider that closure is the best solution for your case because you are only passing a function, usually I use the protocols when I use more functions in which I want to delegate the responsibility