我對swift代碼比較陌生。不能陣列的文字傳遞的plist到表視圖...無法將數組名稱傳入Swift 3的表視圖中
現在我得到一個空表視圖在模擬器
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>
有什麼'self.chapters'輸出? –
嘗試一次''self.chapters = self.ansTopics.keys.sorted()'line'self.tableView.reloadData()''self'tableView.reloadData()' –
@NiravD空模擬器 – Nish