Reputation: 4015
I creating a tableview programatically and each tableview cell have buttons associated to them.
If I click the row I can work out the tag associated with the buttons on that row and then able to edit the title when applying some other functions.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
TmpActionConnectorTag = indexPath.row + 700 //Tag associated to button
}
When the button is clicked , I have this code which changes the other button on that row
let tag = TmpActionConnectorTag
let tmpButton = self.view.viewWithTag(tag) as? UIButton
The issue is if I directly click on the button in the tableview cell, the did select row does not get call and no tag value is given. To do it I have to click within the cell first and then the button to be able to know the tag associated with the row.
Is there a way to workout the index row when the button is clicked so I don't have to click the actual cell?
above is how the cell looks normally and below shows how the cell has to be selected to be able to get the index value.
Upvotes: 1
Views: 7134
Reputation: 11
Disabling the button's User Interaction Control control worked for me.
Programmatically:
editButton.isUserInteractionEnabled = false
Upvotes: -1
Reputation: 3549
Disabling the button's control worked for me.
Programmatically:
editButton.isEnabled = false
~OR~
IB:
Uncheck the Enabled box in the Control section.
Upvotes: 0
Reputation: 2246
The cell's button will not trigger func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
instead you need to add a target
to your button which is typically done within cellForItemAt
.
Add target to button within cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = MyTableViewCell()
cell.button.tag = indexPath.row
cell.button.addTarget(self, action: #selector(didTapCellButton(sender:)), for: .touchUpInside)
}
Handle button action
@objc func didTapCellButton(sender: UIButton) {
guard viewModels.indices.contains(sender.tag) else { return } // check element exist in tableview datasource
//Configure selected button or update model
}
Upvotes: 1
Reputation: 1284
Button action won't call the didSelectRowAt
. You should go for delegate method. If not aware about delegate means refer this
Upvotes: 2