【问题标题】:How can I 'speak' to a var through a string我如何通过字符串“说话”到 var
【发布时间】:2016-07-27 13:23:20
【问题描述】:

我真的不知道如何比标题更好地描述我的问题,但我希望你能用这段代码理解它(倒数第二行显示我想要做什么,但我不知道该怎么做斯威夫特):

var titlesZeneggen = ["Zeneggen", "Dienstleistungen", "Erlebnis", "Gastronomie", "Unterkunft", "Kalender", "Multimedia", "Wetter/Webcams", "Orte"]
var titlesDienstleistungen = ["Zeneggen", "Dienstleistungen", "Erlebnis", "Gastronomie", "Unterkunft", "Kalender", "Multimedia", "Wetter/Webcams", "Orte"]
var titlesErlebnis = ["Zeneggen", "Dienstleistungen", "Erlebnis", "Gastronomie", "Unterkunft", "Kalender", "Multimedia", "Wetter/Webcams", "Orte"]
var titlesGastronomie = ["Hotel Alpenblick", "Bistro", "in der Nähe"]

var seguedTitle = ""

override func viewDidLoad() {
    self.navigationItem.title = seguedTitle
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("secondMenuCellUI")! as UITableViewCell

    cell.selectionStyle = UITableViewCellSelectionStyle.None
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

    cell.textLabel?.text = var(named: "titles" + self.seguedTitle)[indexPath.row]

    return cell

}

我希望我的标题更改为 seguedTitle。 seguedTitle 由前一个视图给出。可能的字符串 seguedTitle 可以是:“Zeneggen”、“Dienstleistungen”、“Erlebnis”等。

希望有人可以帮助我。

【问题讨论】:

  • 没有语言功能可以做到这一点。您需要以不同的方式构建应用程序。
  • 我可以用一些'if'然后'else if'语句来做到这一点,但是代码真的很长。真的没有可能吗?
  • 用字典怎么样?然后您可以使用字符串键查找值。
  • 或带开关的枚举
  • 我可以将数组添加到字典中的键吗? (一键多值)

标签: xcode string swift var


【解决方案1】:

我认为这是一种方法,可以满足您的要求。

var titles[
    "Zeneggen" : ["Zeneggen", "Dienstleistungen", "Erlebnis", "Gastronomie", "Unterkunft", "Kalender", "Multimedia", "Wetter/Webcams", "Orte"],
    "Dienstleistungen" : ["Zeneggen", "Dienstleistungen", "Erlebnis", "Gastronomie", "Unterkunft", "Kalender", "Multimedia", "Wetter/Webcams", "Orte"],
    "Erlebnis" : ["Zeneggen", "Dienstleistungen", "Erlebnis", "Gastronomie", "Unterkunft", "Kalender", "Multimedia", "Wetter/Webcams", "Orte"],
    "Gastronomie" :["Hotel Alpenblick", "Bistro", "in der Nähe"]
]

var seguedTitle = ""

override func viewDidLoad() {
    self.navigationItem.title = seguedTitle
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // Make sure that the title is really there.
    if let tableData = titles[self.seguedTitle] as? [String] {
        return tableData.count
    }
    return 0
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("secondMenuCellUI")! as UITableViewCell

    cell.selectionStyle = UITableViewCellSelectionStyle.None
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

    if let tableData = titles[self.seguedTitle] as? [String] {
        cell.textLabel?.text = tableData[indexPath.row]
    }

    return cell
}

【讨论】:

  • 谢谢。不错的解决方案!我今晚会试试这个。
猜你喜欢
  • 1970-01-01
  • 2015-05-08
  • 2023-04-05
  • 1970-01-01
  • 2013-03-15
  • 2016-12-13
  • 2017-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多