如何在swift 3的表视图中显示数据? [英] How to display data in table view in swift 3?

查看:22
本文介绍了如何在swift 3的表视图中显示数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我需要将数据传递给表格视图以显示并得到很多错误任何人都可以帮助我如何实现这里键应该显示为部分标题标题并且内部值应该显示在行中

Here I need to pass the data to the table view to display and getting lots of errors can anyone help me how to implement this here keys should be displayed as section header title and inside values should be displayed in rows

这里是数组的声明

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
    }

推荐答案

对于初学者,更新以下数据源方法

For starters, update the following datasource methods

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
}

这篇关于如何在swift 3的表视图中显示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆