TableView数组节自己重新排序? (程序修复?)

时间:2019-05-15 16:18:41

标签: ios swift uitableview

我按特定顺序列出了数组,但是,当我启动模拟器时,节数组超出了预期的顺序。

我尝试重新排列数组顺序,但无法确定Xcode为何继续重新排列节段

enter image description here

我缺少什么吗?如果是这样,我该如何解决? (所需顺序如下:阵列-船->火车->飞机->汽车

1 个答案:

答案 0 :(得分:1)

根据定义,字典是无序的。 viewDidLoad中的顺序未定义。我建议直接创建对象数组,而不是从字典中创建

let  transportMeans : [String: [String]] = ["Boats" : ["Tugboat", "Tanker"], "Trains" : ["Bullet", "Freight", "Commuter"],"Planes" : ["Fighter Jet", "Cargo Jet", "Commercial Jet"], "Cars" : ["Coupe", "Truck", "Mini"]]

override func viewDidLoad() {
    super.viewDidLoad()

    objectArray = [Objects(sectionName: "Boats", sectionObjects: ["Tugboat", "Tanker"], sectionImages: ["Tugboat", "Tanker"]),
                   Objects(sectionName: "Trains", sectionObjects: ["Bullet", "Freight", "Commuter"], sectionImages: ["Bullet", "Freight", "Commuter"]),
                   Objects(sectionName: "Planes", sectionObjects: ["Fighter Jet", "Cargo Jet", "Commercial Jet"], sectionImages: ["Fighter Jet", "Cargo Jet", "Commercial Jet"]),
                   Objects(sectionName: "Cars", sectionObjects: ["Coupe", "Truck", "Mini"], sectionImages: ["Coupe", "Truck", "Mini"])
    ]
}

然后– once again从不将结构成员声明为隐式未包装的可选内容,请删除感叹号

var sectionObjects : [String]

注意:sectionImages数组是多余的。您也可以从sectionObjects获取图像