简体   繁体   English

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

[英]TableView array sections reordering themselves? (programmatic fix?)

I list my array in a specific order-however, when I launch simulator, the section array is out of intended order. 我以特定顺序列出了数组,但是,当我启动模拟器时,节数组却超出了预期的顺序。

I have tried rearranging the array order but can't pin why Xcode keeps rearranging the sections 我已经尝试过重新排列数组顺序,但是无法确定为什么Xcode会继续重新排列节

在此处输入图片说明

Is there something I am missing? 我有什么想念的吗? If so, how can I fix? 如果是这样,我该如何解决? (desired order is as listed in array: Boats -> Trains -> Planes -> Cars (所需的顺序列在阵列中:船->火车->飞机->汽车

A dictionary is unordered by definition. 根据定义,字典是无序的。 The order in viewDidLoad is undefined. viewDidLoad的顺序未定义。 I recommend to create the array of objects directly rather than from a dictionary 我建议直接创建对象数组,而不是从字典中创建

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

And – once againnever declare struct members as implicit unwrapped optional, remove the exclamation mark 并且- 再一次 - 永远不要将结构成员声明为隐式解包的可选,删除感叹号

var sectionObjects : [String]

Note: the sectionImages array is redundant. 注意: sectionImages数组是多余的。 You can get the image from sectionObjects , too 您也可以从sectionObjects获取图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM