【问题标题】:Displaying dictionary values each in a UITableViewCell在 UITableViewCell 中显示每个字典值
【发布时间】:2017-06-13 16:57:23
【问题描述】:

我的总体目标是创建一个 UITableView,它根据字典中的值具有按字母顺序排列的部分标题。

我有一个包含水果名称和描述的基本对象。我希望每个水果的名称都显示在 UITableCell 上,在部分标题的对应字母下方。

最终,tableview 将如下所示:(其中 C P S 是节标题)

C------------
Clementine
Cherry
Coconut
P------------
Pear
Peach
S------------
Strawberry

这是我的数据:

var fruitModel = [String:Any]()


fruitModel = [
    "C" : [
    FruitModel(name: "Clementine", description: "Clementines are super tasty!"),
    FruitModel(name: "Cherry", description: "Cherry's are red."),
    FruitModel(name: "Coconut", description: "Nuts for coconuts!!!"),
    ],
    "P" : [
    FruitModel(name: "Pear", description: "Pears rock!"),
    FruitModel(name: "Peach", description: "Mmmm, Peach."),
    ],
    "S" : [
    FruitModel(name: "Strawberry", description: "A widely grown hybrid species of the genus Fragaria. It is cultivated worldwide for its fruit.")
    ]
]

让我失望的部分是访问字典值中的名称字符串,并将其显示在 cellForRowAt 中。下面的代码是我卡住的地方:

cellForRowAt:

var allValues = Array(fruitModel.values)
cell.fruitNameLabel.text = (allValues[indexPath.row] as AnyObject).name // Cannot subscript a value of type 'inout Array<Any>'

一旦我弄清楚如何在每个单元格中显示水果名称,我就可以让部分标题正常工作。我只需要帮助解决我遇到的错误。谢谢。

【问题讨论】:

    标签: ios swift uitableview dictionary


    【解决方案1】:

    请参阅Swift Dictionary: Get values as array,了解您在索引方面遇到的错误。

    但是,关于单元格的顺序,您将面临一个更大的问题,因为不能保证字典值按照添加顺序返回。

    字典将相同类型的键和相同类型的值之间的关联存储在没有定义排序的集合中。

    因此,如果您希望它们按字母顺序显示,则需要重新考虑正在使用的数据结构。

    【讨论】:

    • “没有定义的排序”是匕首,嗯?当。你会推荐什么结构?请记住,我真正想要完成的只是按字母顺序排列的表格视图,并带有部分标题。我只是认为字典最容易管理我的模型。
    • 一个包含元组的数组就可以了。 [(String, [FruitModel])] 这样您就可以保持一致的订单。
    • 只保留一个数组,它是字典的排序键。
    • 您也可以这样做,以便将排序映射到数据。
    • 我正在一步一步来。在解决排序之前,我仍然无法从单元格中的值显示名称。我检查了“Swift Dictionary: Get values as array”链接,并用我收到的新错误更新了我的原始帖子。任何帮助表示赞赏。谢谢。
    【解决方案2】:

    swift

    中的表格视图中显示字典值数组
    var arrayDictionary = [[String:Any]]()
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cellTable = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
                cellTable.labelTitle.text = arrayDictionary[indexPath.row]["name"] as? String
                cellTable.labelAuthor.text = arrayDictionary[indexPath.row]["region"] as? String
                cellTable.labelDescription.text = arrayDictionary[indexPath.row]["subregion"] as? String
                return cellTable
            }
    

    【讨论】:

      【解决方案3】:

      这里是您查询的完整示例

      class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
      
      struct FruitModel {
          var name: String
          var description: String
      }
      var fruitModel = [String: [FruitModel]]()
      var dataKeys : Array = [String]()
      
      
      @IBOutlet weak var tableView: UITableView!
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
          tableView.delegate = self
          tableView.dataSource = self
          fruitModel = [
              "C" : [
              FruitModel(name: "Clementine", description: "Clementines are super tasty!"),
              FruitModel(name: "Cherry", description: "Cherry's are red."),
              FruitModel(name: "Coconut", description: "Nuts for coconuts!!!"),
              ],
              "P" : [
              FruitModel(name: "Pear", description: "Pears rock!"),
              FruitModel(name: "Peach", description: "Mmmm, Peach."),
              ],
              "S" : [
              FruitModel(name: "Strawberry", description: "A widely grown hybrid species of the genus Fragaria. It is cultivated worldwide for its fruit.")
              ]
          ]
      
          dataKeys = (fruitModel as NSDictionary).allKeys as! [String]
          print(dataKeys)
          tableView.reloadData()
      
      }
      
      func numberOfSections(in tableView: UITableView) -> Int {
          return dataKeys.count
      }
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          let sec = dataKeys[section]
          return fruitModel[sec]?.count ?? 0
      }
      func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
          return dataKeys[section]
      }
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
          let sec = dataKeys[indexPath.section]
          cell.textLabel?.text = fruitModel[sec]?[indexPath.row].name
      
          return cell;
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-15
        • 2022-01-15
        • 2017-01-27
        • 2011-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多