使用 plist 中的数据填充 tableView 行

Populating tableView rows with data from plist

免责声明:swift 编码新手,但对编码世界并不陌生

获取设备作为部分header;但无法将公司作为 tableView 中的行。 我应该如何让公司名称填充到与设备类型对应的 tableView 行中

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

plist 文件

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

尝试更改此代码,

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

像这样,

let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier)

请试试这个。

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

这是更新后的代码:

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