【问题标题】:swift, convert NSIndexpath to Int迅速,将 NSIndexpath 转换为 Int
【发布时间】:2015-03-17 14:31:09
【问题描述】:

我有一个表格视图,每个单元格中都有一个按钮。当它被点击时,我有一个 didselectrow 函数。我想使用我的 indexpath 从提供单元格内容的数组中获取一个值。如何将此 NSIndexpath 转换为 int 以在数组中使用?

【问题讨论】:

  • indexPath.Row。 IndexPath 是一个 NSIndexPath。

标签: arrays uitableview int didselectrowatindexpath


【解决方案1】:

您最需要的是 row 属性。你可以看到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)

}

【讨论】:

  • 具有讽刺意味的是,我在自己寻找答案时发现了您的问题。
【解决方案2】:

使用 IndexPath.row,例如下面的例子。

参考(索引路径: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

示例

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

参考

【讨论】:

    猜你喜欢
    • 2015-11-13
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    相关资源
    最近更新 更多