使用plist中的数据填充tableView行

时间:2017-01-06 05:12:41

标签: ios swift uitableview swift3 plist

免责声明:快速编码的新手,但不是编码世界的新手

将设备作为节标题;但无法将公司作为tableView中的行。 我应该如何在与设备类型

对应的tableView行中填充公司名称
27

plist文件

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

3 个答案:

答案 0 :(得分:0)

请试试这个。

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

答案 1 :(得分:0)

这是更新的代码:

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

答案 2 :(得分:-1)

尝试更改此代码,

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

let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier)