这里我需要将数据传递到表视图以显示并获取大量错误任何人都可以帮助我如何实现这里,键应显示为节标题,内部值应显示在行
这是数组的声明
var finalDict = [String: [String]]()
数组的输出如下所示
["Flat Rate": ["Fixed", "Base", "Fixed two"], "Best Way": ["Worldwide Express Saver one", "Table Rate", "Worldwide Expedited", "Worldwide Express Saver"]]
以下是表格视图的代码
func numberOfSections(in tableView: UITableView) -> Int {
return finalDict.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let names = self.finalDict.keys
return names[section]
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.tintColor = UIColor.white
header.textLabel?.textColor = UIColor.darkGray
header.textLabel?.textAlignment = NSTextAlignment.left
header.textLabel?.font = UIFont(name: "Futura", size: 17)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let names = self.finalDict.keys
let a :[Any] = finalDict[names] as! [Any]
return a.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "shippingCell", for: indexPath) as! ShippingMethodTableViewCell
let key = self.keys[indexPath.section]
progressIcon.stopAnimating()
progressIcon.hidesWhenStopped = true
var a :[Any] = arrayss[key] as! [Any]
var dictionary = a[indexPath.row] as! [String:Any]
let price = dictionary ["price"]
let name = dictionary["name"]
let namePrice = "\(name!)" + " \(price!)"
cell.methodLabel.text = namePrice
cell.radioButton.addTarget(self, action: #selector(paymentRadioAction(button:)), for: .touchUpInside)
if chekIndex == indexPath {
cell.radioButton.isSelected = true
} else {
cell.radioButton.isSelected = false
}
return cell
}
答案 0 :(得分:0)
首先,更新以下数据源方法
func numberOfSections(in tableView: UITableView) -> Int {
return finalDict.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let titles = Array(finalDict.keys)
return titles[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let titles = Array(finalDict.keys)
let currentTitle = titles[section]
let values = finalDict[currentTitle] as! [String]
return values.count
}
答案 1 :(得分:0)
请尝试将self.finalDict.keys
替换为Array(self.finalDict.keys)
。