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

时间:2017-09-14 12:41:04

标签: ios swift uitableview 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"]]

这里是我的函数这对于有多行的单个部分工作正常问题是我不知道如何使用索引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 个答案:

答案 0 :(得分:0)

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

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