─░lkin Elimov ─░lkin Elimov - 1 year ago 121
Swift Question

How to get a value of selected row in Swift?

I have a Table View Controller in my swift application. How to I get a value of selected row (cell)?

For example, I want to show a value of selected row with alert when the user clicked on row.

Answer Source

Just implement the method:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var alert = UIAlertView()
    alert.delegate = self
    alert.title = "Selected Row"
    alert.message = "You selected row \(indexPath.row)"
    alert.addButtonWithTitle("OK")
    alert.show()
}

This delegate method gets fired whenever you select a row. Via NSIndexPath you get the exact section and row the user selected. In order to make sure the method gets called you need to make sure your tableview's delegate is set.

But since you are using an UITableViewController this should already be done automatically for you.

For further documentation see: tableView:didSelectRowAtIndexPath

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download