Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift, convert NSIndexpath to Int

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?

like image 407
Vincent Avatar asked Jan 19 '15 14:01

Vincent


2 Answers

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)

}
like image 186
Ogre Codes Avatar answered Nov 15 '22 02:11

Ogre Codes


Use IndexPath.row, see below for example.

Ref (IndexPath:316-319)

/// The row of this index path, when used with `UITableView`.
///
/// - precondition: The index path must have exactly two elements.
public var row: Int

Example

let name : String = (indexPath.row>0) ? "Bob" : "Sam";

Reference

  • IndexPath - Foundation | Apple Developer Documentation
  • row - IndexPath | Apple Developer Documentation
like image 30
J-Dizzle Avatar answered Nov 15 '22 03:11

J-Dizzle