I have a tableview with a variable number of cells representing students that correspond to their particular instructor. They are custom cells with a button that triggers a segue to a new VC, bringing up detailed information on the student whose cell it was. My question is:
What is the best practice in swift for identifying which button was pressed?
Once i know the index path, I can identify which student's information needs to be passed to the next VC. There is a great answer for objective C in the post below, but I'm not sure how to translate to Swift. Any help would be much appreciated.
Detecting which UIButton was pressed in a UITableView
If you want to check If UIButton is Pressed or not, You should handle TouchDown Handler and change button's state to pressed in touchDown hadnler. You can track ToucUpInside method to Change state of button to Not-pressed again. Thank you so much. By using the addTarget syntax I solved my problem!
add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.
In order to populate the tableview, you need to: In the storyboard, set your view controller to be the datasource of the tableview by dragging the "datasource" connection from the connections menu of the table view to your view controller. Make your view controller conform to the UITableViewDataSource protocol.
In this blog post, I'll introduce the collection view Table in SwiftUI and explain how to leverage this view on iOS 16 to build a multiplatform app. SwiftUI provides several collection views that you can use to assemble other views into dynamic groupings with complex, built-in behaviors.
If your code allows, I'd recommend you set the UIButton
tag equal to the indexPath.row
, so when its action is triggered, you can pull the tag and thus row out of the button data during the triggered method. For example, in cellForRowAtIndexPath
you can set the tag:
button.tag = indexPath.row button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
then in buttonClicked:
, you can fetch the tag and thus the row:
func buttonClicked(sender:UIButton) { let buttonRow = sender.tag }
Otherwise, if that isn't conducive to your code for some reason, the Swift translation of this Objective-C answer you linked to:
- (void)checkButtonTapped:(id)sender { CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition]; if (indexPath != nil) { ... } }
is:
func checkButtonTapped(sender:AnyObject) { let buttonPosition = sender.convert(CGPoint.zero, to: self.tableView) let indexPath = self.tableView.indexPathForRow(at: buttonPosition) if indexPath != nil { ... } }
Swift 3.0 Solution
cell.btnRequest.tag = indexPath.row cell.btnRequest.addTarget(self,action:#selector(buttonClicked(sender:)), for: .touchUpInside) func buttonClicked(sender:UIButton) { let buttonRow = sender.tag }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With