从Swift中的单个Dictionary填充tableview的不同部分

时间:2021-07-23 15:40:36

I am new to iOS development & just having problem to populate data in different sections from single dictionary.here is my dictionary

我是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"]]

here is my function this works fine for a single section with multiple rows the problem is I don't know how to use index path.section in this

这里是我的函数这个工作正常的单个部分有多行问题是我不知道如何使用索引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"]!)
}

1 个解决方案

#1


0  

Group the elements that should be in the same section in array like this

将应该在数组中的同一部分中的元素分组,如下所示

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"]
            ]
        ]

you can access them with

你可以用它来访问它们

cell.lblCityName.text!=menuItems[indexPath.section][indexPath.row]["city"]!

#1


0  

Group the elements that should be in the same section in array like this

将应该在数组中的同一部分中的元素分组,如下所示

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"]
            ]
        ]

you can access them with

你可以用它来访问它们

cell.lblCityName.text!=menuItems[indexPath.section][indexPath.row]["city"]!