Reputation: 83
Lets say I have 10 buttons in a custom UITableViewCell. How do I identify which button is tapped and perform the respective action back in ViewController which holds the cell? I am looking for a optimistic solution in swift. Thank you
Upvotes: 0
Views: 840
Reputation: 352
//Put this code in your UITableViewDataSource: cellForRowAt
//Cell must contains these buttons on which you need add do addTarget
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "currentTableViewCell", for: indexPath)
as! CurrentTableViewCell
cell.btn1.tag = 1
cell.btn1.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)
cell.btn2.tag = 2
cell.btn2.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)
cell.btn3.tag = 3
cell.btn3.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)
return cell
}
@objc func buttonPressed(sender: UIButton) {
let convertedPointInTable = sender.convert(CGPoint.zero, to:self.currentTableView)
let retriveIndexPath = self.currentTableView.indexPathForRow(at: convertedPointInTable)
print("In which cell \(retriveIndexPath!.row), which button pressed \(sender.tag)")
}
Upvotes: 1
Reputation: 54
**There two ways to do that =:
First you can make collection of IB button outlets and define tag.
Second One is same but instead of making collection of outlets, go to your storyboard and on click button, you will see tag option there, for each button give different tag(suppose if you have 10 button give tag 1 to 10) Since you are doing it under custom cell, you can either do above things inside UITableViewCell class or you can preform target under cellForRowAt also, both will work fine
// Now inside your button action do this -:
if sender.tag == 1{
print("one")
}
if sender.tag == 2{
print("two")
}
//OR (if inside cellForRowAt)
cell.button.addTarget(self, action:#selector(handleRegister), for: .touchUpInside)
*so on...
//Make sure sender type in IBAction must be UIButton
// you can also put this under switch cases, just basic programming*
Upvotes: 0
Reputation: 61
Cell code
final class MyCell: UITableViewCell {
struct ConfiguringData {
let action1: () -> Void
let action2: () -> Void
}
@IBOutlet private weak var button1: UIButton!
@IBOutlet private weak var button2: UIButton!
private var didTapButton1Action: (() -> Void)?
private var didTapButton2Action: (() -> Void)?
@IBAction private func didTapButton1() {
didTapButton1Action?()
}
@IBAction private func didTapButton2() {
didTapButton2Action?()
}
func configure(with configuringData: ConfiguringData) {
didTapButton1Action = configuringData.action1
didTapButton2Action = configuringData.action2
}
}
ViewController code
class MyViewController: UIViewController {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell
let action1 = { print("didTapButton1") }
let action2 = { print("didTapButton2") }
myCell.configure(
with: MyCell.ConfiguringData(action1: action1, action2: action2)
)
return myCell
}
}
Upvotes: 0