Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

ios - How to display data in table view in 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

Here is the declaration for the array

var finalDict = [String: [String]]()

the output of the array is shown below

["Flat Rate": ["Fixed", "Base", "Fixed two"], "Best Way": ["Worldwide Express Saver one", "Table Rate", "Worldwide Expedited", "Worldwide Express Saver"]]

Here is the code for table view

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
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...