【问题标题】:Populate different sections of tableview from single Dictionary in Swift从 Swift 中的单个 Dictionary 填充 tableview 的不同部分
【发布时间】:2017-09-14 12:41:03
【问题描述】:

我是 iOS 开发新手,只是在从单个字典中填充不同部分的数据时遇到问题。这是我的字典

var menuItems = [["city":"Lahore","cityimage":"2","country":"Pakistan"],["city":"istanbul","cityimage":"3","country":"Turkey"],["city":"Moscow","cityimage":"4","country":"Russia"],["city":"Riyadh","cityimage":"5","country":"KSA"]]

这是我的函数,它适用于具有多行的单个部分,问题是我不知道如何在其中使用 index path.section

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell=tableView.dequeueReusableCell(withIdentifier: "TableCell", for:indexPath) as! TableCellTry
    cell.lblCityName.text!=menuItems[indexPath.row]["city"]!
    cell.lblProvince.text!=menuItems[indexPath.row]["country"]!
    cell.imgCity.image=UIImage(named:menuItems[indexPath.row]["cityimage"]!)
}

【问题讨论】:

  • 哪些元素应该在 #0 部分,哪些在 #1 部分?
  • 第 0 部分的前 2 个城市和第 1 部分的后 2 个城市
  • 那么@Michael 的answer 是对的。

标签: ios swift uitableview dictionary


【解决方案1】:

像这样将应该在同一节中的元素分组

var menuItems = [
            [
                ["city":"Lahore","cityimage":"2","country":"Pakistan"],
                ["city":"istanbul","cityimage":"3","country":"Turkey"]
            ],
            [
                ["city":"Moscow","cityimage":"4","country":"Russia"],
                ["city":"Riyadh","cityimage":"5","country":"KSA"]
            ]
        ]

您可以使用

访问它们
cell.lblCityName.text!=menuItems[indexPath.section][indexPath.row]["city"]!

【讨论】:

    猜你喜欢
    • 2015-06-17
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多