除非用户触摸,否则tableview不显示数据

时间:2015-03-17 09:27:30

标签: ios uitableview swift

我正在尝试在我的tableview上显示一些数据,但如果我不触摸tableview,它就不会显示数据。它只在我触摸后显示数据。

我做了一些研究但是找不到与我的问题类似的东西。以下是我的代码。我该如何解决这个问题?

import UIKit

class ViewController: UIViewController {

    @IBOutlet var myTableView: UITableView!

    var myData:NSMutableArray = NSMutableArray()

    func getManufacturer2(){
        var url : String = "http://00.00.000.000/myWebService"
        var request : NSMutableURLRequest = NSMutableURLRequest()
        request.URL = NSURL(string: url)
        request.HTTPMethod = "GET"

        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
            var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
            let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary

            if (jsonResult != nil) {

                var userMomentList:NSMutableArray = NSMutableArray()

                self.myData = jsonResult.objectForKey("cevap") as NSMutableArray

                self.myTableView.reloadData()

            } else {
                // couldn't load JSON, look at error
                println("jsonResult is nil")
            }
        })
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        getManufacturer2()
    }

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if(self.myData.count > 0) {
            return myData.count
        }
        return 0;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "myTableCell")
        var data = self.myData.objectAtIndex(indexPath.row) as NSDictionary
        cell.textLabel?.text = data.objectForKey("item_title") as NSString
        cell.detailTextLabel?.text = data.objectForKey("item_value") as NSString

        return cell
    }


}

2 个答案:

答案 0 :(得分:4)

用户界面上的更新必须始终在主线程中完成,因此请尝试包装tableView重新加载:

dispatch_async(dispatch_get_main_queue(), {
    self.myTableView.reloadData()
})

答案 1 :(得分:0)

您将返回Zero作为numberOfRowsInSection。