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>
Ü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 } –
@HimanshuMoradiya hinzugefügt firmename.count aber nicht in der Lage, die Daten noch zu bekommen – Dar
Bitte kopieren und fügen Sie echten Code. – rmaddy