2017-08-03 61 views
0

我正在從多個「向下鑽取」tableViews顯示來自plist的數據。顯示的數據只會是隻讀的,我不希望它必須基於Web數據,並且最多隻有大約100個數據點,所以我對plist而不是JSON感到滿意。在桌面視圖中顯示來自plist的數據數組

無論如何,這個問題...我有一個字典項目的數組,我管理顯示相當好。我從GitHub開始,關於堆棧溢出的下列問題(我修改了一下,但那不重要)。

Load data from a plist to two TableViews

https://github.com/DonMag/SWPListData

我需要什麼顯示的是項目的每個人下一個數組(在這個例子中的「朋友」)的幫助。下面的代碼將有望解釋。

我已經創造了在的viewController模型

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

//This is the new array I've added and am having trouble displaying 
let friends: [String] 


init(dictionary: [String: Any]) { 
    self.functionary = (dictionary["Functionary"] as? String) ?? "" 
    self.imageFace = (dictionary["ImageFace"] as? String) ?? "" 
    self.phone = (dictionary["Phone"] as? String) ?? "" 

    //I've initialised it here. 
    self.friends = (dictionary["Friends"] as? [String]) ?? [""] 

空數組現在顯示的數據。我沒有任何問題顯示「功能」,「imageFace」和「電話」的正確數據 - 但我似乎無法在自己的部分中顯示「朋友」作爲數組。我敢肯定,最主要的問題是在numberOfRowscellForRow

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if section == 0 { 
    if let theEmployee = newPage { 
     return theEmployee.details.count 
    } 
    return 0 

} 
    else if section == 1 { 
     return 1 
    } 
    else if section == 2 { 
     if let theEmployee = newPage { 
      return theEmployee.details.count 
     } 
     return 0 
    } 
    else { 
     return 0 
    } 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) // as! TableViewCell2 

    if indexPath.section == 0 { 

     cell.textLabel?.text = "A" 

     if let theEmployee = newPage { 
      cell.textLabel?.text = theEmployee.details[indexPath.row].functionary 
      cell.detailTextLabel?.text = theEmployee.details[indexPath.row].phone + " (" + theEmployee.details[indexPath.row].imageFace + ")" 
     } 
    } 

    else if indexPath.section == 1 { 
     cell.textLabel?.text = "Test" 

     } 

    else if indexPath.section == 2 { 
     cell.textLabel?.text = ???? 

     } 

    return cell 
} 

我認爲這會工作編寫以下numberOfRows

else if section == 2 { 
     if let theEmployee = newPage { 
      return theEmployee.details.friends.count 
     } 
     return 0 
    } 

但我得到的錯誤:

value of type '[EmployeeDetails]' has no member 'friends'.

我需要做些什麼來獲得該數組?請注意:該數組不是空的。

任何建議/幫助將不勝感激!

+0

我覺得你應該寫這樣theEmployee.details [yourIndex] .friends.count – 3stud1ant3

回答

0

問題是,你正在訪問的詳細信息的朋友財產,而它是保存在裏面的細節,所以你必須通過索引的東西來訪問它像這樣

theEmployee.details[yourIndex].friends.count 

更新的結構屬性:

//首先,你需要做的.plist文件中的變化,請去那裏和添加好友裏面就像詳細

陣列現在,這將要求您更改員工結構代碼

struct Employee { 
    let position: String 
    let name: String 
    let details: [EmployeeDetails] 
    let friends: [String] 

    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])}) 
self.friends = (dictionary["Friends"] as? [String]) ?? [] 

    } 
} 

下一步將是在表視圖中添加此如下

override func numberOfSections(in tableView: UITableView) -> Int { 
return 2 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

if let theEmployee = newPage { 
if section == 0 { 
return theEmployee.details.count 
}else if section == 1 { 
return theEmployee.friends.count 
} 

} 
return 0 

    } 
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

if section == 0 { 
return "Subordinates" 
}else if section == 1 { 
return "Friends" 
} 

return "" 
} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) // as! TableViewCell2 

cell.textLabel?.text = "A" 

if let theEmployee = newPage { 
if indexPath.section == 0 { 
cell.textLabel?.text = theEmployee.details[indexPath.row].functionary 
cell.detailTextLabel?.text = theEmployee.details[indexPath.row].phone + " (" + theEmployee.details[indexPath.row].imageFace + ")" 
}else if indexPath.section == 1 { 
cell.textLabel?.text = theEmployee.friends[indexPath.row] 
} 
} 



return cell 
    } 

} 
+0

OK啊。那麼,我將在哪裏/如何爲「yourIndex」聲明和初始化數組。 – DrewDog

+0

我是否需要爲每位員工(或工作人員)分配一個唯一的字典鍵/值,然後在代碼中提及? – DrewDog

+0

@DrewDog我做了一些修改並更新了答案,請檢查我的答案,並詢問是否需要進一步幫助 – 3stud1ant3

相關問題