简体   繁体   English

表格视图没有加载swift

[英]table view not loading with swift

I have trouble in loading the table view when parsing json files in swift. 在swift中解析json文件时,我在加载表视图时遇到了麻烦。

Parsing the data is doing well. 解析数据表现良好。 But no data are displayed in the table view. 但表视图中不显示任何数据。

This is the code : 这是代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var redditListTableView: UITableView!

    var tableData = []

    @IBAction func cancel(sender: AnyObject) {
        self.dismissViewControllerAnimated(false, completion: nil)
        println("cancel")
    }
    @IBAction func done(sender: AnyObject) {
        println("done")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        searchJsonFile("blabla.json")
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        println(tableData.count)
        return tableData.count
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")

        let rowData: NSString = self.tableData[indexPath.row] as NSString
        cell.textLabel.text = rowData as String

        return cell
    }

    func searchJsonFile(searchFile: String) {

        let urlPath = "http://data.../\(searchFile)"
        let url = NSURL(string: urlPath)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
            println("Task completed")
            if(error != nil) {
                // If there is an error in the web request, print it to the console
                println(error.localizedDescription)
            }
            var err: NSError?

            var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary

            if(err != nil) {
                println("JSON Error \(err!.localizedDescription)")
            }
            var results = [String]()

            if let results1 = jsonResult["data"] as? NSDictionary{
                for (key, value) in results1 {
                    if let eng = value["eng"] as? NSDictionary {
                        if let name = eng["name"] as? NSString{
                            results.append(name)
                        }
                    }
                }
            }

            //println(results)  OK!!!!

            dispatch_async(dispatch_get_main_queue(), {
                self.tableData = results
                self.redditListTableView.reloadData()
            })

        })

        task.resume()

    }

}

You are returning 0 from numberOfSectionsInTableView - so you get no data displayed. 您从numberOfSectionsInTableView返回0 - 因此您不会显示任何数据。 You want 1 section - 你想要1节 -

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

If you are not having sections then just remove this function or comment 如果您没有部分,那么只需删除此功能或评论

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

or else return 1 或者返回1

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

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