【问题标题】:Implement a TableView with Sections and populating it with data from a struct使用 Sections 实现 TableView 并使用结构中的数据填充它
【发布时间】:2018-11-14 08:19:56
【问题描述】:

我对 iOS 和 Xcode 开发人员大多是新手,所以我现在只是在这里要求一些帮助......

我正在制作一个带有医疗症状表格视图的应用。我已经设法用数组中的数据填充表格视图,并将其分成取决于医学专业的部分。但是,关于能够在表中选中多个症状并将其保存以供以后在另一个 ViewController 中使用,我遇到了障碍。

我最容易理解的方法是创建一个包含整个症状列表的字典,然后根据是否选中每个症状为每个症状分配一个布尔值。但我不知道如何实现一个包含所有数据的结构,以及如何将其分成多个部分。

我还想知道将所有数据保存在外部文件中并允许应用访问数据是否更好。

如果需要,我可以添加我目前编写的代码。

我们将不胜感激!

下面是我当前的数据源实现

    let SymptomsSections = ["General", "Respiratory", "Cardiac", "Abdominal", "Neurological", "Psychiatrical"]

    let SymptomsList =
        [
                ["Dry mouth", "Malaise", "Asthenia"],
                ["Dyspnoea", "Wheezing"],
                ["Orthopnoea", "Angina"],
                ["Coffee ground vomiting", "Melaena"],
                ["Agnosia", "Apraxia"]
        ]

以及在tableView中编写它的代码

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
    return self.SymptomsSections[section]
}

override func numberOfSections(in tableView: UITableView) -> Int
{
    return self.SymptomsSections.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return SymptomsList[section].count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "SymptomsCell", for: indexPath)

    cell.textLabel?.text = SymptomsList[indexPath.section][indexPath.row]
    return cell
}

【问题讨论】:

  • 您需要将这些数据保存在本地吗?每次应用程序启动时显示?还是每次打开应用程序时创建它们?
  • 我希望数据存储在本地,列表保持不变,并在应用启动时显示
  • 你知道在本地存储数据是一个很大的话题,我建议你每次用户打开应用程序时先创建,然后在你有能力自己做之后,你可以跳到下一步,存储它们。
  • @JeremyHao-YangChoi 如果您要使用 CoreData,请查看我的问题,因为我整理了一个必须遵循的分步指南。 stackoverflow.com/questions/56168119/…

标签: arrays swift xcode struct tableview


【解决方案1】:

更新答案

我运行了 OP 共享的代码,发现它崩溃了。这样做的主要原因是因为SymptomsSectionsSymptomsList 的内容数量不同,因此导致out of index range crash 用于较小的数组,在我们的例子中是SymptomsList。对于解决方法,我添加了行SymptomsList.indices.contains(index),它基本上在访问之前检查给定索引处是否存在任何对象。这解决了我们的崩溃。然后我继续更新字典和访问方法。还附上输出屏幕供您理解。

请通过代码

let SymptomsSections = ["General", "Respiratory", "Cardiac", "Abdominal", "Neurological", "Psychiatrical"]

let SymptomsList = [[["name":"Dry mouth", "checked" : true], ["name":"Malaise", "checked" : false], ["name":"Asthenia", "checked" : true]],
                    [["name":"Dyspnoea", "checked" : true], ["name":"Wheezing", "checked" : false]],
                    [["name":"Orthopnoea", "checked" : false], ["name":"Angina", "checked" : true]],
                    [["name":"Coffee ground vomiting", "checked" : true], ["name":"Melaena", "checked" : true]],
                    [["name":"Agnosia", "checked" : false], ["name":"Apraxia", "checked" : true]]]


func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return self.SymptomsSections[section]
    }


func numberOfSections(in tableView: UITableView) -> Int {
    return self.SymptomsSections.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if SymptomsList.indices.contains(section) {
        return SymptomsList[section].count
    } else {
        return 0
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "SymptomsCell", for: indexPath)
    if SymptomsList.indices.contains(indexPath.section) {
        cell.textLabel?.text = SymptomsList[indexPath.section][indexPath.row]["name"] as? String
        if SymptomsList[indexPath.section][indexPath.row]["checked"] as! Bool {
            cell.accessoryType = UITableViewCellAccessoryType.checkmark
        } else {
            cell.accessoryType = UITableViewCellAccessoryType.none
        }
    } else {
        cell.textLabel?.text = nil
       cell.accessoryType = UITableViewCellAccessoryType.none
    }

    return cell
}

原答案

好吧,要展示一些你需要知道的东西。不确定您是如何获取数据的。但我可以想象你的 tableview 数据源看起来像这样:

let symptoms = ["fever", "cold", "heart ache"]

当您说最简单的方法是使用字典时,您是对的。想象一下,更新后的数据源是一个字典数组,如下所示:

let symptoms = [["name":"fever", "checked":true], ["name":"cold", "checked":true], ["name":"heart ache", "checked":false]]

cellForRow 方法中分配单元格时。您可以使用以下内容:

cell.titleLabel.text = symptoms[indexPath.row]["name"] 
if symptoms[indexPath.row]["checked"] {
   cell.accessoryType = UITableViewCellAccessoryType.checkmark
} else {
   cell.accessoryType = UITableViewCellAccessoryType.none
}

【讨论】:

  • 我已经附上了我的代码目前的样子。我知道您是如何创建字典数组的,但我能否将其实现到带有部分的 tableView 中?
  • @JeremyHao-YangChoi 是的,当然为什么不呢!假设您的代码按您预期的方式工作。然后在我看来,你只需要对你共享的代码库做一些小的改动。第一个更改是更新SymptomsList 以保存字典。第二个更改是cell.textLabel?.text = SymptomsList[indexPath.section][indexPath.row] 行,说明您将如何访问它。
  • @JeremyHao-YangChoi 我已经更新了答案以解决您的特定代码中面临的问题。如果这个或任何其他答案可以帮助您实现您想要实现的目标,请记住将它们标记为正确,以便将来对其他人有所帮助。
猜你喜欢
  • 2020-06-06
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-24
  • 1970-01-01
相关资源
最近更新 更多