Search code examples
iosswiftuitableviewswift3plist

Populating tableView rows with data from plist


Disclaimer: New to swift coding but not new to coding world

Getting the devices as the section header; but not able to get companies as the rows inside the tableView. How should i get the company names to be populated inside the tableView rows corresponding to the devices type

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

The plist file

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

Solution

  • Here is updated code:

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