简体   繁体   English

swift,将NSIndexpath转换为Int

[英]swift, convert NSIndexpath to Int

I have a tableview with a button in each cell. 我有一个tableview,每个单元格都有一个按钮。 When that is tapped I have a didselectrow function. 当点击它时,我有一个didselectrow功能。 I want to use my indexpath to get a value from an array that gives the cells contents. 我想使用我的indexpath从一个给出单元格内容的数组中获取一个值。 How can I convert this NSIndexpath to an int to use in the array? 如何将此NSIndexpath转换为要在数组中使用的int?

Most likely what you want is the row property. 你最想要的是row属性。 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)

}

Use IndexPath.row, see below for example. 使用IndexPath.row,请参阅下面的示例。

Ref (IndexPath:316-319) 参考 (索引路径: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 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM