【问题标题】:table view not loading with swift表格视图未快速加载
【发布时间】:2015-01-02 12:56:23
【问题描述】:

我在快速解析 json 文件时无法加载表格视图。

解析数据做得很好。但是表格视图中不显示任何数据。

这是代码:

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()

    }

}

【问题讨论】:

    标签: ios json uitableview swift


    【解决方案1】:

    您从 numberOfSectionsInTableView 返回 0 - 因此您没有显示任何数据。你想要 1 个部分 -

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

    【讨论】:

    • 哦!!!太感谢了。我昨天一直在努力,并尝试了很多解决方案......问题解决了!!!!非常感谢。
    【解决方案2】:

    如果您没有部分,则只需删除此功能或评论

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

    否则返回 1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-18
      • 2021-06-05
      • 2015-06-01
      • 2016-09-28
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多