2017-01-06 3 views
0

Disclaimer: Neu in Swift-Codierung, aber nicht neu in Coding WorldTabelle füllen Zeilen mit Daten von plist anzeigen

Abrufen der Geräte als Abschnittsüberschrift; aber nicht in der Lage, Unternehmen als die Zeilen innerhalb der TableView zu erhalten. Wie sollte ich erhalten die Firmennamen in den Tableview Zeilen an den Geräte-Typ

import UIKit 

class MainPageViewController: UITableViewController { 
var devices = [AnyObject]() 

let CellIdentifier = "Cell Identifier" 

var companyName: [AnyObject] { 
    if let companyName = devices["Comp"] as? [AnyObject] { 
     return companyName 
    } else { 
     return [AnyObject]() 
    } 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    title = "Devices" 

    let filePath = Bundle.main.path(forResource: "Array", ofType: "plist") 
    if let path = filePath { 
     devices = NSArray(contentsOfFile: path) as! [AnyObject] 
    } 
    print(device) 
    tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: CellIdentifier) 
} 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return devices.count 
} 

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    var titleName = title 
    if let deviceHeader = devices[section] as? [String: AnyObject], let titled = deviceHeader["Device"] as? String 
    { 
     titleName = titled 
    } 
    return titleName 
} 

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) 
    if let company = companyName[indexPath.row] as? [String: AnyObject], let name = company["Company"] as? String { 
     cell.textLabel?.text = name 
    } 

    return cell; 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

Die plist Datei

<array> 
    <dict> 
     <key>Device</key> 
     <string>Smartphones</string> 
     <key>Comp</key> 
     <array> 
      <dict> 
       <key>Company</key> 
       <string>Apple</string> 
      </dict> 
      <dict> 
       <key>Company</key> 
       <string>Samsung</string> 
      </dict> 
     </array> 
    </dict> 
    <dict> 
     <key>Device</key> 
     <string>Notebooks</string> 
     <key>Comp</key> 
     <array> 
      <dict> 
       <key>Company</key> 
       <string>HP</string> 
      </dict> 
      <dict> 
       <key>Company</key> 
       <string>Dell</string> 
      </dict> 
      <dict> 
       <key>Company</key> 
       <string>Lenovo</string> 
      </dict> 
     </array> 
    </dict> 
</array> 
</plist> 
+0

Überschreibung func Tableview (_ Tableview: UITableView, numberOfRowsInSection Abschnitt: Int) -> Int { // #WARNING unvollständige Umsetzung, geben die Anzahl der Zeilen return 0 // hier hinzufügen einen Wert wie devices.count } –

+0

@HimanshuMoradiya hinzugefügt firmename.count aber nicht in der Lage, die Daten noch zu bekommen – Dar

+0

Bitte kopieren und fügen Sie echten Code. – rmaddy

Antwort

0

Hier ist Code aktualisiert:

var devices = [AnyObject]() 
    let CellIdentifier = "Cell" 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     title = "Devices" 
     let filePath = Bundle.main.path(forResource: "Array", ofType: "plist") 
     if let path = filePath { 
      devices = NSArray(contentsOfFile: path) as! [AnyObject] 
     } 
     tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: CellIdentifier) 
    } 
    // MARK: - Table view data source 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return devices.count 
    } 
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     var titleName = title 
     if let deviceHeader = devices[section] as? [String: AnyObject], let titled = deviceHeader["Device"] as? String 
     { 
      titleName = titled 
     } 
     return titleName 
    } 
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     guard let device = devices[section] as? [String: AnyObject], let companies = device["Comp"] as? [AnyObject] else { 
      return 0 
     } 
     return companies.count 
    } 
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) 

     guard let device = devices[indexPath.section] as? [String: AnyObject], let companies = device["Comp"] as? [AnyObject] else { 
      return cell 
     } 
     guard let company = companies[indexPath.row] as? [String: AnyObject] else { 
      return cell 
     } 
     cell.textLabel?.text = company["Company"] as? String 
     return cell; 
    } 
+0

Es hat perfekt funktioniert ... Ich habe Zweifel, warum Guard let verwendet wird? – Dar

+0

Guard wird verwendet, um verschachtelte Strukturen zu vermeiden. Sie können mehr hier lesen https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html –

+0

was wird der Code, wenn ich comp direkt als ein Array von Zeichenfolgen mit den Firmennamen machen ? – Dar

-1

diesen Code, wie

let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) 

ändern Versuchen entsprechend gefüllt werden dies,

let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) 
+0

nicht funktioniert ... Wirft viele Fehler – Dar

0

versuchen Sie es bitte.

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    let dict = devices[section] 
    let array = dic["comp"]  
    return array.count 
} 

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

    let dict = devices[section] 
    let array = dic["comp"] 
    if let companyname = array[indexPath.row] { 
     cell.textLabel?.text = companyname 
    } 
    return cell; 
} 
+0

zurück Array.Count zwingt mich, in Return-Array konvertieren !!. Count – Dar

+0

und der func tableView funktioniert nicht; Das Anzeigen eines Fehlers kann einen Wert von tyep anyobject mit einem Index vom Typ Abschnitt – Dar

+0

@Dar nicht subskribieren. Zuerst habe ich versucht, Device Dictionary nach Abschnitt zu bekommen, nach dem Versuch, eine Reihe von Unternehmen durch 'comp' Schlüssel zu bekommen Array von Unternehmen, die ich bekommen kann der erforderliche Firmenname von indexPath.row. Hoffentlich verstehst du meine Vorgehensweise. Wenn Sie das Problem lösen können, indem Sie meinen vorgeschlagenen Code aktualisieren, tun Sie es bitte. –