2017-03-28 44 views
1

我有一些麻煩從我的屬性列表中加載一些數據。看看... 這是我directory.plistdirectory.plist將數據從plist加載到兩個TableViews

我想告訴所有這一切,就在這裏Main.storyboardMain.storyboard

但鍵PositionËName將出現在第一TableViewController和密鑰Functionary,ImageFacePhone必須出現在第二個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 
        } 
    } 
    

當我在第一TableViewController的任何物品觸碰,我看到第二TableViewController完全空了!和調試區域顯示它:

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

回答

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的工作示例。

+0

我剛更新了這個問題。 –

+1

嗯......好的......你明白我的回答嗎?當它是一個字典的「數組」時,你會將'details'當作'Dictionary'處理。在我的回答中看到編輯 – DonMag

+0

現在我有下面的錯誤信息:對Functionary和ImageFace的印刷品上'成員下標'的歧義引用。 –