【问题标题】:Displaying an array of data from plist in tableview在 tableview 中显示来自 plist 的数据数组
【发布时间】: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”和“电话”的正确数据 - 但我似乎无法在其自己的部分中将“朋友”显示为数组。我很确定主要问题出在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
    } 

但我得到了错误:

“[EmployeeDetails]”类型的值没有成员“朋友”。

我需要做什么才能得到那个数组?

注意:数组不为空。

任何建议/帮助将不胜感激!

【问题讨论】:

  • 我觉得你应该这样写 theEmployee.details[yourIndex].friends.count

标签: ios arrays swift uitableview plist


【解决方案1】:

问题是您正在访问有关详细信息的朋友属性,而它是存储在详细信息中的结构上的属性,因此您必须通过像这样的索引来访问它

theEmployee.details[yourIndex].friends.count

更新:

//首先你需要在.plist文件里面做一些修改,请去那里添加Friends Array里面就像Details一样

现在这将要求您更改 Employee 结构代码

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
    }

 }

【讨论】:

  • 嗯,好的。那么我将在哪里/如何为“yourIndex”声明和初始化数组。
  • 我是否需要在 plist 中为每个员工(或工作人员)分配一个唯一的字典键/值,然后在代码中引用它?
  • @DrewDog 我做了一些更改并更新了答案,请检查我的答案,并询问您是否需要进一步的帮助
  • 是的!这正是我所追求的。非常感谢你的帮助。我赞成你的回答,但我还没有足够的分数。
  • 别担心,很高兴我能帮上忙
猜你喜欢
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多