Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将数据从一个plist加载到两个tableview_Ios_Swift_Xcode_Uitableview_Plist - Fatal编程技术网

Ios 将数据从一个plist加载到两个tableview

Ios 将数据从一个plist加载到两个tableview,ios,swift,xcode,uitableview,plist,Ios,Swift,Xcode,Uitableview,Plist,我在从属性列表中加载一些数据时遇到了一些问题。看看… 这是我的目录。plist: 我想在Main.storyboard上展示所有这些: 但是键PositioneName将出现在第一个TableViewController上,键functional、ImageFace和Phone必须出现在第二个TableViewController上 为此,我做了如下: 向AppDelegate添加了以下内容: func application(_ application: UIApplication, di

我在从属性列表中加载一些数据时遇到了一些问题。看看… 这是我的
目录。plist

我想在
Main.storyboard
上展示所有这些:

但是键
Position
e
Name
将出现在第一个TableViewController上,键
functional
ImageFace
Phone
必须出现在第二个TableViewController

为此,我做了如下:

  • 向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
    
  • 创建了一个结构,如下所示:

    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] = []
    }
    
    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)
            }
        }
    }
    
  • 我的第一个TableViewController是这样的:

    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] = []
    }
    
    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)
            }
        }
    }
    
  • 我的第二个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
        }
    }
    
  • 当我触摸第一个TableViewController的任何项目时,我看到第二个TableViewController完全为空!调试区域显示:

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

    根据您的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)
    
        })
    
    举个例子


    有关工作示例,请参阅。

    Umm。。。好啊你明白我的答案了吗?当
    是字典的
    数组时,您将
    细节
    视为一个
    字典
    。请参见我的中的编辑answer@ArnonRodrigues-看来你已经上路了。我更新了我的答案,稍微更改了原始结构,使您能够更“一致”地访问数据。也许更好,也许不是:)@Arnnrodrigues-你很接近。。。我继续写了一个GItHub repo,这样你就可以看一看——比试图解释什么要容易得多:)根据调试/错误消息,你的故事板中似乎有什么东西没有正确连接。在Interface Builder中仔细检查分配给每个UITableViewController的类。如果您看到的是表2,但它是“空的”,那么它的行数是否正确?这听起来也像是情节提要中的混乱。。。细胞原型可能缺少ID?是的,IB中的东西可能会“混乱”。打开你的主。情节提要。。。选择“工作人员”场景。。。将类(在Identity Inspector下)从
    Page2
    更改为普通的
    UITableViewController
    。。。按Command+B以生成。。。再次选择工作人员场景。。。将类更改回第2页。。。运行应用程序,然后。。。。呜呼?