2016-12-16 10 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がstringのサブクラスでない場合、この行は失敗します。

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

は、上記の「タイトル」セクションのプロパティは、オブジェクトを示唆あなたは「titleString」プロパティとして設定するために探しているものです:私はあなたを想定してい

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

は次のように何かをしたいです次のView Controllerの

元の質問とは無関係に、「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のを考えていました:/ –

関連する問題