Ios Swift-使用[String:[String:String]]的字典填充uitableview

Ios Swift-使用[String:[String:String]]的字典填充uitableview,ios,swift,uitableview,dictionary,Ios,Swift,Uitableview,Dictionary,我是Swift的新手,目前正在创建一个日记应用程序,向用户提问。我存储用户的输入,如下所示: dict = ["date": ["question1": "answer", "question2": "answer"]] 现在我需要在tableview中向用户显示这些数据,其中“date”是标题,“question1”是描述 我在网上查过了,但答案似乎引用了“indepath.row”将信息输入到单元格中,但因为这是一个字符串字典,所以我不能这样做 谢谢你的帮助 您可以使用map进行尝试。这里

我是Swift的新手,目前正在创建一个日记应用程序,向用户提问。我存储用户的输入,如下所示:

dict = ["date": ["question1": "answer", "question2": "answer"]]
现在我需要在tableview中向用户显示这些数据,其中“date”是标题,“question1”是描述

我在网上查过了,但答案似乎引用了“indepath.row”将信息输入到单元格中,但因为这是一个字符串字典,所以我不能这样做


谢谢你的帮助

您可以使用
map
进行尝试。这里
字典
转换为字典数组

let dict = ["date": ["question1": "answer", "question2": "answer"]]

if let value = dict["date"] {
    let v = value.map {
        ["question": $0.key, "answer": $0.value]
    }

    debugPrint(v)
}

在显示正在使用的字典类型中的数据之前,您必须做一些准备工作。还要记住,字典不是顺序列表,所以数据的打印顺序完全取决于系统。一种方法是:

var data = ["date1":["q1":"A1","q2":"A2","q3":"A3"],"date2":["q1":"A1","q2":"A2","q3":"A3"]] . //This is data from your example
var displayableData = [(title: String, qAndA: [(question: String, answer: String)])]() //this is what we will be needing


override func viewDidLoad() {
    super.viewDidLoad()

    //convert the whole dictionary to tuple
    displayableData = data.map { ($0.key, $0.value.map{ ($0.key, $0.value)})}

    //here we have converted the dictionary to what we need
}


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

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

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 55.0
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    let currentQA = displayableData[indexPath.section].qAndA[indexPath.row]
    cell.textLabel?.text = "\(currentQA.question) -> \(currentQA.answer)"
    return cell
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 30.0
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 30.0))
    view.backgroundColor = UIColor.lightGray
    let label = UILabel(frame: CGRect(x: 10, y: 0, width: view.bounds.width - 20, height: 30.0))
    label.text = displayableData[section].title
    view.addSubview(label)
    return view
}

而不是使用字典数组,应该考虑使用更好地表示数据的对象。

struct Question: {
    let question: String
    let answer: String
}

struct DiaryDay {
    let date: Date // Note this is a Date object, not a String
    let questions: [Question]
}
那你有

let diaryDays = DiaryDay(date: <date>, questions: 
    [Question(question: "question1": answer: "answer"),
     Question(question: "question2": answer: "answer")])
然后每个问题一行

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let diaryDay = diaryDays[section]
    return diaryDay.questions.count
}
然后配置你的手机

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // dequeue cell
    let diaryDay = diaryDays[indexPath.section]
    let question = diaryDay.questions[indexPath.row]    
    cell.question = question
    return cell
}
并在节标题中显示日期

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let diaryDay = diaryDays[section]

    return // formatted diaryDay.date
}

您可能需要查看和的文档。这些是用于为表视图提供数据和交互逻辑的协议。
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let diaryDay = diaryDays[section]

    return // formatted diaryDay.date
}