2016-04-06 3 views
-3

私の質問をタイトルよりもよく表現する方法は本当に分かりませんが、このコードで理解することを願っています(2番目に低い行は、行うには、しかし、私は)スウィフトで行う方法がわからない:文字列を通して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"などです。

誰かが私を助けることを望みます。

+0

これを行う言語機能はありません。あなたは、あなたのアプリを別に構造化する必要があります。 – nielsbot

+0

私はいくつかの 'if'と 'else if'ステートメントでそれを行うことができますが、その後コードは本当に長くなります。本当にそれを行う可能性はありますか? –

+0

辞書を使うのはどうですか?次に、文字列キーを使用して値を検索できます。 – nielsbot

答えて

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 
} 
+0

ありがとうございました。ニースの解決策!私は今夜​​これを試してみる。 –

関連する問題