【问题标题】:How can I access an item from an array dynamically with Swift如何使用 Swift 动态访问数组中的项目
【发布时间】:2016-02-28 04:37:19
【问题描述】:

我有一个数组,用于填充 UITableView 的行。

选择一行后,我需要根据选择的行从数组中检索信息以填充一些出口(标签、文本字段等)

例如:

我在 ViewController 的 didSelectRowAtIndexPath 中为我设置为 indexPath.row 的 TableView 创建了一个 itemSelected 变量

itemSelected = indexPath.row

然后在我的 otherViewController 的 viewDidLoad 中,我需要通过

检索信息
array[itemSelected]

但是,我收到一个编译器错误:“表达式解析为未使用的 i 值”

【问题讨论】:

  • 您需要在 prepareForSegue 方法中将对该对象的引用发送到下一个视图控制器。

标签: ios arrays swift uitableview dynamic


【解决方案1】:

在这里,您只需访问数组但不调用任何值。例如,如果您的数组中有一个名为“Name”的键,并且您想将其设置为 UILabel,请按此操作。

self.Name.text = array[itemSelected].valueForKey("Name") as! String

如果不只是用它做点什么。

self.Name.text = array[itemSelected] as! String

print(array[itemSelected])

【讨论】:

    猜你喜欢
    • 2021-03-12
    • 1970-01-01
    • 2020-05-09
    • 2013-05-26
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    相关资源
    最近更新 更多