2016-12-16 4 views
2

Ich habe Probleme beim Weitergeben von Daten aus der ausgewählten Zeile in meinem TableView an den anderen ViewController. DieseTableView Array-Segment wird keine Daten übergeben

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) 
     } 

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

Es wäre bevorzugt, wenn der Titel und Objekte wurden separat geführt.

Dies ist meine Daten-Setup:

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 

Antwort

0

Diese Linie legt nahe, Sie String in Abschnitte unterklassiert. Warum untergliedern Sie String? Wenn Sections keine Unterklasse von string ist, schlägt diese Zeile fehl.

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

ich Sie gehe davon möchte etwas ähnlich wie diese:

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

Die oben schlägt einen „Titel“ Eigenschaft auf den Abschnitten Objekt ist das, was man als „titleString“ Eigenschaft zu setzen suchen des nächsten View-Controllers.

Unabhängig von der ursprünglichen Frage sollten Sie "Generell" klein schreiben. Best Practices schlagen vor, dass nur Klassennamen mit einem Großbuchstaben beginnen sollten. Ich empfehle auch, die didSelectRow-Methode zu implementieren, um Ihre Daten auszuwählen. Die Ergebnisse sollten sieht wie folgt aus:

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' eine Struktur ist und daher nicht – Sweeper

+0

Oops subclassed werden kann ... Ich war NSString denken: / –

Verwandte Themen