我正在努力解决以下问题:
我正在尝试设计和读取一个包含UITableViewController信息的数组。
表视图将有几个部分,每个部分都有几个单元格,每个单元格都有一个名称和一个开/关开关。
在stackoverlfow的帮助下,我能够到达下面的这一点,除了UISwitch的默认位置之外,我拥有最多的东西。
import UIKit
class SettingsTableViewController: UITableViewController {
var sections = ["Panels:": ["Radar", "ADIRs", "Transponder"], "Engine Type:": ["IAE", "CFM", "NEO"]]
struct Objects {
var sectionName: String!
var sectionObjects: [String]!
}
var objectArray = [Objects]()
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
for (key, value) in sections {
print("\(key) \(value)")
objectArray.append(Objects(sectionName: key, sectionObjects: value))
}
navigationItem.title = "Settings"
tableView.register(PanelCell.self, forCellReuseIdentifier: cellId)
tableView.allowsSelection = false
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "< BACK", style: .plain, target: self, action: #selector(self.navigateBack))
}
func navigateBack() {
self.dismiss(animated: true, completion: nil)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectArray[section].sectionObjects.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! PanelCell
cell.nameLabel.text = objectArray[indexPath.section].sectionObjects[indexPath.row]
cell.panelSwitch.isOn = true
cell.settingsTableViewController = self
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectArray[section].sectionName
}
}
...所以为了包含UISwitch的初始位置,我试图设计一个不同的数组:
var sections:[(sectionTitle: String, [(itemTitle: String, defaultValue: Bool)])] = [
("Panels", [("Radar",false),("ADIRs",false),("Transponder",false)]),
("Engine Type", [("IAE",false),("CFM",true),("NEO",false)])
]
我修改了Objects结构,如下所示:
struct Object {
var sectionName: String!
var items: [(title: String, onOff: Bool)]!
}
但是我无法弄清楚如何将所有这些读入Object(s)数组。
var objectArray = [Object]()
var object = Object()
for index in sections {
print("\(index)")
object.sectionName = index[0].sectionTitle
}
上面的最后一行给出了一个错误。如果我尝试像这样访问它,它可以工作:
print("\(sections[0].sectionTitle)")
print("\(sections[0].1[0].itemTitle)")
print("\(sections[0].1[0].defaultValue)")
print("\(sections[0].1[1].itemTitle)")
print("\(sections[0].1[1].defaultValue)")
print("\(sections[0].1[2].itemTitle)")
print("\(sections[0].1[2].defaultValue)")
所以我不知道如何正确地将这个数组读入objectArray。我也有直觉,我对问题的整体解决方法都是错误的。
感谢您的帮助!
答案 0 :(得分:1)
我更新了一些代码以使其正常工作:
maxOccurs=length_of_colorName_list
<强> EDITED 强>
为了避免字典排序不确定的问题,数据结构可以是(作为JSON格式):
typealias CellObject = (title: String, onOff: Bool)
class SettingsTableViewController: UITableViewController {
var sections: [String: [CellObject]] = [
"Panels:":
[("Radar", true),
("ADIRs", false),
("Transponder", false)
],
"Engine Type:": [
("IAE", true),
("CFM", false),
("NEO", false)
]
]
struct Objects {
var sectionName: String
var sectionObjects: [CellObject]
}
var objectArray = [Objects]()
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
for (key, value) in sections {
print("\(key) \(value.count)")
objectArray.append(Objects(sectionName: key, sectionObjects: value))
}
navigationItem.title = "Settings"
tableView.register(PanelCell.self, forCellReuseIdentifier: cellId)
tableView.allowsSelection = false
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "< BACK", style: .plain, target: self, action: #selector(self.navigateBack))
}
func navigateBack() {
self.dismiss(animated: true, completion: nil)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return objectArray.count // don't use sections object anymore
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectArray[section].sectionObjects.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! PanelCell
let item = objectArray[indexPath.section].sectionObjects[indexPath.row]
cell.nameLabel.text = item.title
cell.panelSwitch.isOn = item.onOff
// cell.settingsTableViewController = self // you should not pass controller to the cell
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectArray[section].sectionName
}
}
数据类可以定义为:
[
{
"title": "Panels",
"items": [
{
"name": "Radar",
"selected": true
},
{
"name": "ADIRs",
"selected": false
},
{
"name": "Transponder",
"selected": false
}
]
},
{
"title": "Engine Type",
"items": [
{
"name": "IAE",
"selected": true
},
{
"name": "CFM",
"selected": false
},
{
"name": "NEO",
"selected": false
}
]
}
]
然后您的数组将是class Data {
var title: String?
var items: [Detail]?
}
class Detail {
var name: String?
var selected: Bool?
}
的类型。将JSON字符串转换为var objectArray = [Data]()
并设置为objectArray(Google或搜索此网站以了解如何将JSON字符串转换为数组)。
如果数据必须返回字典,您需要与您的客户或团队成员讨论订购字典键的最佳方法,以确保数据按预期存在。