Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 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 Answer

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...