简体   繁体   English

将JSON解析为TableView

[英]Parse JSON to TableView

I'm trying to parse my JSON response to my TableView. 我正在尝试解析对TableView的JSON响应。 The problem is that I don't get any result in my TableView. 问题是我的TableView没有得到任何结果。

import UIKit
import Alamofire
import SwiftyJSON

class MenuViewController: UITableViewController {

    var products: [Product] = []

    // MARK: View Controller Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        Alamofire.request(.GET, Urls.menu).responseJSON { request in
            if let json = request.result.value {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
                    let data = JSON(json)
                    var product: [Product] = []

                    for (_, subJson): (String, JSON) in data {
                        product += [Product(id: subJson["id"].int!, name: subJson["name"].string!, description: subJson["description"].string!, price: subJson["price"].doubleValue)]
                    }

                    dispatch_async(dispatch_get_main_queue()) {
                        self.products += product
                        self.tableView.reloadData()
                    }
                }
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - UITableViewDataSource

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return self.products.count
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }

    private struct Storyboard {
        static let CellReuseIdentifier = "Product"
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(Storyboard.CellReuseIdentifier, forIndexPath: indexPath)
        let product = self.products[indexPath.row]

        cell.textLabel?.text = product.name
        cell.detailTextLabel?.text = product.description

        return cell
    }

I debugged my numberOfSectionsInTableView and I got this result: 我调试了numberOfSectionsInTableView并得到了以下结果:

0
0
0
0
0
0
5

Five is the total of items in my JSON request, but my View is never updated. JSON请求中的项目总数为五,但我的视图从未更新。 I was watching some classes about Swift, and in one of them, I see the teacher showing this method self.tableView.reloadData() but didn't work for me, or at least, I'm doing it wrong. 我正在看一些有关Swift的课程,在其中一个课程中,我看到老师展示了此方法self.tableView.reloadData()但对我没有用,或者至少我做错了。

The rest of the code, I believe is correct, but without the reload of the data, I can't show it in my tableView. 我相信其余的代码是正确的,但是如果没有重新加载数据,就无法在tableView中显示它。

Thank you. 谢谢。

You've mixed up numberOfRowsInSection with numberOfSections. 您已经将numberOfRowsInSection与numberOfSections混合了。 Return 1 for numberOfSections (or just delete that function entirely, it's optional) and products.count for numberOfRows. 返回1表示numberOfSections(或者只是完全删除该函数,这是可选的),并返回product.count表示numberOfRows。

You mixed up numberOfSectionsInTableView and numberOfRowsInSection 您混合了numberOfSectionsInTableViewnumberOfRowsInSection

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.products.count
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM