2016-12-16 55 views
2

我無法將數據從TableView中的選定行傳遞到另一個ViewController。TableView數組segue不會傳遞數據

class SectionsTableViewController: UITableViewController { 


var sections: [Sections] = SectionsData().getSectionsFromData() 

override func viewDidLoad() { 
    super.viewDidLoad() 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return sections.count 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return sections[section].items.count 
} 


override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return sections[section].headings 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "sectionCell", for: indexPath) 

    // Configure the cell... 
    cell.textLabel?.text = sections[indexPath.section].items[indexPath.row] 

    return cell 
} 



override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
{ 
    if (segue.identifier == "sectionCell") 
    { 
     let upcoming: Sjekkliste = segue.destination as! Sjekkliste 

     let indexPath = self.tableView.indexPathForSelectedRow! 

     let titleString = Sections(title: "", objects: [""]) as? String 

     upcoming.titleString = titleString 

     self.tableView.deselectRow(at: indexPath, animated: true) 
     } 

這是我的問題是:let titleString = Sections(title: "", objects: [""]) as? String

如果標題和對象是分別通過這將是首選。

這是我的數據設置:

class SectionsData { 
var myArray: [AnyObject] = [] 

func getSectionsFromData() -> [Sections] { 

    var sectionsArray = [Sections]() 

    let Generell = Sections(title: "Animals", objects: 
     ["Cat", "Dog", "Lion", "Tiger"]) 
    sectionsArray.append(Generell) 
    return sectionsArray 

回答

0

此行表明你的子類的字符串段。你爲什麼繼承String?如果Sections不是字符串的子類,那麼這條線會失敗。

let titleString = Sections(title: "", objects: [""]) as? String 

我假設你想與此類似:

let selectedSection:Sections = Sections(title: "", objects: [""]) 
upcoming.titleString = selectedSection.title 

以上建議一個「稱號」的章節屬性對象你正在尋找設置爲「titleString」屬性是什麼下一個視圖控制器。

與原始問題無關,您應該小寫「Generell」。最佳做法建議只有班級名稱應以大寫字母開頭。我也建議實施didSelectRow方法來選擇你的數據。結果應該類似於此:

var selectedSection:Sections? //goes at top 

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    tableView.deselectRow(at: indexPath, animated: true) 
    self.selectedSection = self.sections[indexPath.section] 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?){ 
if (segue.identifier == "sectionCell"){ 
    let upcoming: Sjekkliste = segue.destination as! Sjekkliste 
    if let section = self.selectedSection{ 
     upcoming.titleString = section.title 
    } 
} 
} 
+0

'String'是一個結構,因此不能被繼承 – Sweeper

+0

哎呀......我在想的NSString:/ –