将数据从plist加载到两个TableView

时间:2017-03-28 15:20:18

标签: ios swift xcode uitableview plist

我在从属性列表中加载一些数据时遇到了一些麻烦。看一看… 这是我的directory.plistdirectory.plist

我想在Main.storyboard上显示所有这些内容: Main.storyboard

但键Position e Name将显示在第一个 TableViewController 和键FunctionaryImageFacePhone上必须出现在第二个 TableViewController

为此,我做了这个:

  1. 添加到 AppDelegate 以下内容:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        if let url = Bundle.main.url(forResource: "directory", withExtension: "plist"), let array = NSArray(contentsOf: url) as? [[String:Any]] {
            Shared.instance.employees = array.map{Employee(dictionary: $0)}
    }
    return true
    
  2. 像这样创建Struct

    struct EmployeeDetails {
        let functionary: String
        let imageFace: String
        let phone: String
    
        init(dictionary: [String: Any]) {
            self.functionary = (dictionary["Functionary"] as? String) ?? ""
            self.imageFace = (dictionary["ImageFace"] as? String) ?? ""
            self.phone = (dictionary["Phone"] as? String) ?? ""
        }
    }
    
    struct Employee {
        let position: String
        let name: String
        let details: [EmployeeDetails] // [String:Any]
    
        init(dictionary: [String: Any]) {
        self.position = (dictionary["Position"] as? String) ?? ""
        self.name = (dictionary["Name"] as? String) ?? ""
    
        let t = (dictionary["Details"] as? [Any]) ?? []
        self.details = t.map({EmployeeDetails(dictionary: $0 as! [String : Any])})
        }
    }
    
    struct Shared {
        static var instance = Shared()
        var employees: [Employee] = []
    }
    
  3. 我的第一个TableViewController ,如下所示:

    class Page1: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let anEmployee = Shared.instance.employees[1]
    
        print("Name:", anEmployee.name)
        print("Position:", anEmployee.position)
    
        anEmployee.details.forEach({
    
            print("Functionary:", $0.functionary)
            print("ImageFace:", $0.imageFace)
            print("Phone:", $0.phone)
        })
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Shared.instance.employees.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell1
    
            cell.nameLabel.text = Shared.instance.employees[indexPath.row].name
            cell.positionLabel.text = Shared.instance.employees[indexPath.row].position
    
            return cell
        }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? Page2,
            let indexPath = tableView.indexPathForSelectedRow {
            destination.newPage = Shared.instance.employees[indexPath.row].details[indexPath.row]
            tableView .deselectRow(at: indexPath, animated: true)
            }
        }
    }
    
  4. 我的第二个TableViewController ,这里:

    var newPage: EmployeeDetails! //recently added
    
    class Page2: UITableViewController {
    
    var newPage: EmployeeDetails!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        if let theEmployee = newPage {
            self.title = theEmployee.name
        }
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let theEmployee = newPage {
            return theEmployee.details.count
        }
    return 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell2
    
        if let theEmployee = newPage {
    
            cell.faceImage.image = theEmployee.details[indexPath.row].imageFace as? UIImage // did not work
            cell.functionary.text = theEmployee.details[indexPath.row].functionary
            cell.phoneLabel.text = theEmployee.details[indexPath.row].phone
        }
        return cell
        }
    }
    
  5. 当我触摸第一个TableViewController的任何项目时,我看到第二个TableViewController完全空了!调试区域显示它:

    2017-03-28 17:16:28.456 plist sample[7138:425253] Unknown class Page2 in Interface Builder file.
    

1 个答案:

答案 0 :(得分:1)

根据您的plist结构:

Shared.instance.employees[indexPath.row].details

details是一个字典数组。您将其视为字典。

编辑:最初的问题是正确的......解决问题的方法多种多样(如您所见/已完成)。另一种选择,可能有用也可能没有用:

struct EmployeeDetails {
    let functionary: String
    let imageFace: String
    let phone: String

    init(dictionary: [String: Any]) {
        self.functionary = (dictionary["Functionary"] as? String) ?? ""
        self.imageFace = (dictionary["ImageFace"] as? String) ?? ""
        self.phone = (dictionary["Phone"] as? String) ?? ""
    }
}
struct Employee {
    let position: String
    let name: String
    let details: [EmployeeDetails] // [String:Any]

    init(dictionary: [String: Any]) {
        self.position = (dictionary["Position"] as? String) ?? ""
        self.name = (dictionary["Name"] as? String) ?? ""

        let t = (dictionary["Details"] as? [Any]) ?? []
        self.details = t.map({EmployeeDetails(dictionary: $0 as! [String : Any])})
    }
}

struct Shared {
    static var instance = Shared()
    var employees: [Employee] = []
}

然后你可以做你的原创:

    if let url = Bundle.main.url(forResource: "directory", withExtension: "plist"), let array = NSArray(contentsOf: url) as? [[String:Any]] {

        Shared.instance.employees = array.map{Employee(dictionary: $0)}

    }

然后通过以下方式访问数据:

    let anEmployee = Shared.instance.employees[0]

    print("Name:", anEmployee.name)
    print("Position:", anEmployee.position)

    print("Detail 0 Functionary:", anEmployee.details[0].functionary)
    print("Detail 0 ImageFace:", anEmployee.details[0].imageFace)
    print("Detail 0 Phone:", anEmployee.details[0].phone)

    // or

    anEmployee.details.forEach({

        print("Functionary:", $0.functionary)
        print("ImageFace:", $0.imageFace)
        print("Phone:", $0.phone)

    })

仅举例。

有关工作示例,请参阅https://github.com/DonMag/SWPListData