Swift 4中如何将字典数据有序展示在TableView的问答标签中?
我来帮你搞定这个TableView展示问答内容的问题!核心问题其实是你现在用两个分开的字典存储数据,既不好保证展示顺序,也不方便TableView统一处理。咱们一步步来调整:
第一步:创建统一的数据模型
先把问题和对应的选项绑定成一个独立模型,这样后续操作会清晰很多:
struct QuestionItem { let questionText: String let options: [String] }
第二步:将字典转换为有序的模型数组
字典本身是无序的,所以我们要先按key排序,再把对应问题和选项组合成模型数组:
// 假设你的问题字典是questionDict,选项字典是optionDict var questionList: [QuestionItem] = [] // 按Int类型的key排序,保证问题按序号展示 let sortedKeys = questionDict.keys.sorted() for key in sortedKeys { // 确保每个key在两个字典里都有对应数据 if let question = questionDict[key], let options = optionDict[key] { let item = QuestionItem(questionText: question, options: options) questionList.append(item) } }
第三步:配置TableView数据源
接下来修改TableView的数据源方法,用整理好的模型数组来驱动展示:
首先在viewDidLoad里开启自动高度(避免内容被截断):
tableView.dataSource = self tableView.rowHeight = UITableView.automaticDimension tableView.estimatedRowHeight = 120 // 给个预估高度,可根据实际内容调整
然后实现数据源协议方法:
extension YourViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return questionList.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // 假设你的cell标识符是"QuestionCell",内部有questionLabel和optionsLabel两个标签 let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionCell", for: indexPath) as! QuestionCell let currentItem = questionList[indexPath.row] // 给问题加上序号,更符合问答格式 cell.questionLabel.text = "Q\(indexPath.row + 1): \(currentItem.questionText)" // 把选项数组转换成带序号的多行文本,比如"1. 选项A\n2. 选项B" let formattedOptions = currentItem.options.enumerated() .map { " \($0.offset + 1). \($0.element)" } .joined(separator: "\n") cell.optionsLabel.text = formattedOptions // 开启标签换行,避免内容被截断 cell.questionLabel.numberOfLines = 0 cell.optionsLabel.numberOfLines = 0 return cell } }
第四步:优化Cell布局
最后要确保Cell里的两个标签布局正确,让自动高度生效。可以自定义Cell并添加约束:
class QuestionCell: UITableViewCell { // 初始化问题标签 let questionLabel: UILabel = { let label = UILabel() label.font = UIFont.boldSystemFont(ofSize: 16) label.numberOfLines = 0 return label }() // 初始化选项标签 let optionsLabel: UILabel = { let label = UILabel() label.font = UIFont.systemFont(ofSize: 14) label.numberOfLines = 0 label.textColor = .darkGray return label }() override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) setupConstraints() } required init?(coder: NSCoder) { super.init(coder: coder) setupConstraints() } private func setupConstraints() { contentView.addSubview(questionLabel) contentView.addSubview(optionsLabel) // 关闭自动布局转换 questionLabel.translatesAutoresizingMaskIntoConstraints = false optionsLabel.translatesAutoresizingMaskIntoConstraints = false // 添加约束,保证标签上下排列且有合适间距 NSLayoutConstraint.activate([ questionLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 16), questionLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16), questionLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16), optionsLabel.topAnchor.constraint(equalTo: questionLabel.bottomAnchor, constant: 8), optionsLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16), optionsLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16), optionsLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -16) ]) } }
这样调整后,TableView就会按序号依次展示每个问题和对应的选项,完全符合标准的问答格式啦!
内容的提问来源于stack exchange,提问作者Sudhir
相关产品推荐
相关产品推荐

