在桌面视图中显示来自plist的数据数组

问题描述:

我正在从多个“向下钻取”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

问题是,你正在访问的详细信息的朋友财产,而它是保存在里面的细节,所以你必须通过索引的东西来访问它像这样

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