无法将数组名称传递到 Swift 中的 table 视图 3
Not able to pass array name into table view in Swift 3
我对 swift 代码比较陌生。无法将 plist 中的数组文本传递到 table 视图...
现在我在模拟器中得到一个空的 table 视图
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "xml", ofType: "plist")
if let dict = NSDictionary(contentsOfFile: path!), let arrays = dict.object(forKey: "Complete") as? [String: Any] {
self.ansTopics = arrays
self.chapters = self.ansTopics.keys.sorted()
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return chapters.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "mainPage", for: indexPath)
cell.textLabel?.text = chapters[indexPath.row]
return cell
}
这是 plist 文件
<plist version="1.0">
<dict>
<key>Complete</key>
<dict>
<key>Autonomic Nervous System</key>
<array>
<string>Cholinergic</string>
<string>Anticholinergic</string>
</array>
<key>Peripheral Nervous System</key>
<array>
<string>Central relaxants </string>
<string>Peripheral relaxants </string>
</array>
</dict>
<key>Chap</key>
<array>
<string>Autonomic Nervous System</string>
<string>Peripheral Nervous System</string>
</array>
</dict>
</plist>
试试这个代码,它可能对你有帮助
let arrays = dict.object(forKey: "Complete") as? Dictionary<String,AnyObject>
而不是
let arrays = dict.object(forKey: "Complete") as? [String: Any]
谢谢
我对 swift 代码比较陌生。无法将 plist 中的数组文本传递到 table 视图...
现在我在模拟器中得到一个空的 table 视图
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "xml", ofType: "plist")
if let dict = NSDictionary(contentsOfFile: path!), let arrays = dict.object(forKey: "Complete") as? [String: Any] {
self.ansTopics = arrays
self.chapters = self.ansTopics.keys.sorted()
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return chapters.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "mainPage", for: indexPath)
cell.textLabel?.text = chapters[indexPath.row]
return cell
}
这是 plist 文件
<plist version="1.0">
<dict>
<key>Complete</key>
<dict>
<key>Autonomic Nervous System</key>
<array>
<string>Cholinergic</string>
<string>Anticholinergic</string>
</array>
<key>Peripheral Nervous System</key>
<array>
<string>Central relaxants </string>
<string>Peripheral relaxants </string>
</array>
</dict>
<key>Chap</key>
<array>
<string>Autonomic Nervous System</string>
<string>Peripheral Nervous System</string>
</array>
</dict>
</plist>
试试这个代码,它可能对你有帮助
let arrays = dict.object(forKey: "Complete") as? Dictionary<String,AnyObject>
而不是
let arrays = dict.object(forKey: "Complete") as? [String: Any]
谢谢