我如何通过字符串“发声”到var

时间:2016-04-06 21:51:01

标签: xcode string swift var

我真的不知道如何比标题更好地描述我的问题,但我希望你用这个代码来理解它(第二个最低的线显示我想做的,但我不知道该怎么做在斯威夫特):

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”等。

希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

这是一种我认为会做你想做的方法。

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
}