【发布时间】:2017-08-03 02:34:05
【问题描述】:
我正在努力在多个“向下钻取”表视图中显示来自 plist 的数据。显示的数据只是只读的,我不希望它一定基于 Web 数据,并且最多只有大约 100 个数据点,所以我对 plist 而不是 JSON 相当满意。
无论如何,问题...我有一系列字典项目,我设法很好地显示这些项目。我从与以下堆栈溢出问题相关的 GitHub 开始(我对其进行了一些修改,但这并不重要)。
Load data from a plist to two TableViews
https://github.com/DonMag/SWPListData
我需要帮助的是在每个人下显示一组项目(在本例中为“朋友”)。下面的代码有望解释。
我在模型中创建了一个空数组
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]) ?? [""]
现在在 viewController 中显示数据。我完全没有任何问题显示“功能”,“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
}
但我得到了错误:
“[EmployeeDetails]”类型的值没有成员“朋友”。
我需要做什么才能得到那个数组?
注意:数组不为空。
任何建议/帮助将不胜感激!
【问题讨论】:
-
我觉得你应该这样写 theEmployee.details[yourIndex].friends.count
标签: ios arrays swift uitableview plist