swift, convert NSIndexpath to Int

Vincent picture Vincent · Jan 19, 2015 · Viewed 8.3k times · Source

I have a tableview with a button in each cell. When that is tapped I have a didselectrow function. I want to use my indexpath to get a value from an array that gives the cells contents. How can I convert this NSIndexpath to an int to use in the array?

Answer

Ogre Codes picture Ogre Codes · Jan 23, 2015

Most likely what you want is the row property. You can see an example here.

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

    let alert = UIAlertController(title: "Item selected", message: "You selected item \(indexPath.row)", preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction(UIAlertAction(title: "OK",
                                  style: UIAlertActionStyle.Default,
                                handler: {
                                    (alert: UIAlertAction!) in println("An alert of type \(alert.style.hashValue) was tapped!")
                                }))

    self.presentViewController(alert, animated: true, completion: nil)

}