我正在從多個「向下鑽取」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」和「電話」的正確數據 - 但我似乎無法在自己的部分中顯示「朋友」作爲數組。我敢肯定,最主要的問題是在numberOfRows
和cellForRow
:
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'.
我需要做些什麼來獲得該數組?請注意:該數組不是空的。
任何建議/幫助將不勝感激!
我覺得你應該寫這樣theEmployee.details [yourIndex] .friends.count – 3stud1ant3