【发布时间】:2018-02-21 03:19:26
【问题描述】:
我一直在尝试使用 plist 字典中的名字填充表格视图。配置单元格值时出错。
我的代码:
// Created a function to get plist dictionary values
func getSwiftArrayFromPlist() -> (Array<Dictionary<String,String>>) {
let path = Bundle.main.path(forResource: "Sheet1", ofType: "plist")
var arr: NSArray?
arr = NSArray(contentsOfFile: path!)
return (arr as? Array<Dictionary<String,String>>)!
}
override func viewDidLoad() {
super.viewDidLoad()
//var mainGroup = [String]()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let array1 = getSwiftArrayFromPlist().count
return array1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
// Configure the cell...
//var key = Array(self.array.keys)[indexPath.row]
var key = getSwiftArrayFromPlist()["firstname"]
var value = getSwiftArrayFromPlist()[key]
return cell
}
在 var 键处出现错误:
不能用“String”类型的索引为“Array>”类型的值下标
我的目标是在表格视图中获取名字列表(键是“名字”值是实际的名字。
【问题讨论】:
-
尝试从 viewdidload n 存储在数组中调用此方法(getSwiftArrayFromPlist()),然后使用该数组将数据填充到表视图中
标签: swift uitableview dictionary