xardes
xardes

Reputation: 13

How can I get selected item in UITableView

How can I get selected item from UITableView in Swift3? I try this way:

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
    //do something
}

but it isn't work for me.

Upvotes: 1

Views: 3340

Answers (1)

SwiftDeveloper
SwiftDeveloper

Reputation: 7368

First you have to set @IBOutlet to the tableView in your ViewController and set as it's delegate and dataSource to you can see the data an respond to changes in the tableView ;

class YourViewController : UIViewController, UITableViewDelegate, UITableViewDataSource

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self
    }

After

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("You selected cell #\(indexPath.row)!")
    }

You can get selected cell. Ty.

Upvotes: 1

Related Questions