Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detecting uibutton pressed in tableview: Swift Best Practices

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

like image 691
jamike Avatar asked Dec 11 '14 18:12

jamike


People also ask

How can I check if UIButton is pressed?

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!

How do you get the indexPath row when a button in a cell is tapped?

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.

How do you populate a Tableview?

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.

Can we use Tableview in SwiftUI?

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.


2 Answers

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 {         ...     } } 
like image 133
Lyndsey Scott Avatar answered Oct 03 '22 02:10

Lyndsey Scott


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         } 
like image 32
Sourabh Sharma Avatar answered Oct 03 '22 00:10

Sourabh Sharma