如果不触摸屏幕,则无法看到表格视图项目



我的表视图类中存在一些问题,我已经从我的服务器获取了数据,并且可以显示在表视图中,但问题是当我完全从服务器获取数据时,它从未显示在表视图中,直到我触摸屏幕..当我触摸屏幕时,我可以看到我的数据,但我想在获得数据时查看我的数据.. 这是我的类..

import Foundation
import UIKit
class MovementRep : UIViewController , UITableViewDelegate, UITableViewDataSource{

var unitID : Int = 0;
var count : Int = 0;
var dateTime = [String]()
var speed = [Double]()
var event = [String]()
var location = [String]()
var len : Int = 0;
var deviation = [String]()

@IBOutlet weak var ActivityIndicator: UIActivityIndicatorView!

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    print("Movement Report Called");
    print("unit ID = (unitID)")
    var id : String = String(self.unitID);
    print("string id = (id)")

    //Send Server Request
    let myURL = NSURL(string: "<URL>");
    let request = NSMutableURLRequest(URL: myURL!);
    request.HTTPMethod = "POST";


    let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
        data, responce, error in

        self.ActivityIndicator.startAnimating()
        if error != nil{
            print("Error = (error)")
        }
      //  print("Responce = (responce)")

        //    let responceString = NSString(data:data,encodeing:NSTUF8StringEncoding);
        //   print("Responce Data = (responceString)");

        do {
            if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
             //   print("JSONResult = (jsonResult)")
                let size = jsonResult["size"] as? Int
                print("result: (size)");
                self.len = size!;
                if let Users = jsonResult["movementReport"] as? [[String: AnyObject]] {

                    //when you got irrespective response of numberOfRowsInSe...
                    //http://stackoverflow.com/questions/31498531/numberofrowsinsection-is-called-before-alamofire-connection/31498570#31498570
                    self.tableView.reloadData()

                    for name in Users {

                        //if you got null from Json ...
                        if let msg = name["message"] as? NSNull{
                            //print("got (d)")
                            self.location.append("null")
                        }
                        //simple chk
                        if let message = name["message"] as? String
                        {
                            self.location.append(message)

                        }
                        //if you got null from Json ...
                        if let dt = name["dateTime"] as? NSNull{
                            //print("got (d)")
                            self.location.append("null")
                        }
                        //simple chk
                        if let date = name["dateTime"] as? String
                        {
                            self.dateTime.append(date)

                        }
                        //if you got null from Json ...
                        if let sp = name["speed"] as? NSNull{
                            //print("got (d)")
                            self.speed.append(0.0)
                        }
                        //simple chk
                        if let speed = name["speed"] as? Double
                        {
                            self.speed.append(speed)

                        }


                        //if you got null from Json ...
                        if let ev = name["reportText"] as? NSNull{
                            //print("got (d)")
                            self.event.append("null")
                        }
                        //simple chk
                        if let event = name["reportText"] as? String
                        {
                            self.event.append(event)

                        }
                        //if you got null from Json ...
                        if let dev = name["route"] as? NSNull{
                            //print("got (d)")
                            self.deviation.append("--")
                        }
                        //simple chk
                        if let devtion = name["route"] as? String
                        {
                            self.deviation.append(devtion)
                        }
                    }
                }

            }
        } catch let error as NSError {
            print(error.localizedDescription)
        }
    }


    task.resume();
    self.tableView.reloadData();

}
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    print("ViewWillAppearCaled")
   self.tableView.reloadData();
}
override func viewDidAppear(animated: Bool) {
    super.viewWillAppear(animated)
    print("ViewDidAppearCaled")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("table view row")
    return len
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    print("table view cell")
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
    if (len > 0){
    cell.date.text = dateTime[indexPath.row];
    cell.speed.text = "(speed[indexPath.row])"
    cell.position.text = event[indexPath.row]
    cell.location.text = location[indexPath.row]
    cell.deviation.text = deviation[indexPath.row]
         cell.index.text = "(indexPath.row+1)";
         self.ActivityIndicator.stopAnimating()
         return cell
    }
    return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 200.0
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.contentView.backgroundColor = UIColor.clearColor()
    let whiteRoundedView : UIView = UIView(frame: CGRectMake(6, 6, self.view.frame.size.width, 200.0))
    whiteRoundedView.layer.backgroundColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), [1.8, 1.8, 1.8, 1.8])
    whiteRoundedView.layer.masksToBounds = false
    whiteRoundedView.layer.cornerRadius = 3.0
    //whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, 1)
        whiteRoundedView.layer.shadowOpacity = 0.1
        cell.contentView.addSubview(whiteRoundedView)
        cell.contentView.sendSubviewToBack(whiteRoundedView)
    }

}

尝试替换此代码:

override func viewDidLoad() {
    super.viewDidLoad()
    print("Movement Report Called");
    print("unit ID = (unitID)")
    var id : String = String(self.unitID);
    print("string id = (id)")
    self.ActivityIndicator.startAnimating()
    let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
    dispatch_async(backgroundQueue, {
        let myURL = NSURL(string: "<URL>");
        let request = NSMutableURLRequest(URL: myURL!);
        request.HTTPMethod = "POST";
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
        data, responce, error in
        if error != nil{
            print("Error = (error)")
        }
        do {
            if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
                let size = jsonResult["size"] as? Int
                print("result: (size)");
                self.len = size!;
                if let Users = jsonResult["movementReport"] as? [[String: AnyObject]] {
                    for name in Users {
                        //if you got null from Json ...
                        if let msg = name["message"] as? NSNull{
                            //print("got (d)")
                            self.location.append("null")
                        }
                        //simple chk
                        if let message = name["message"] as? String
                        {
                            self.location.append(message)

                        }
                        //if you got null from Json ...
                        if let dt = name["dateTime"] as? NSNull{
                            //print("got (d)")
                            self.location.append("null")
                        }
                        //simple chk
                        if let date = name["dateTime"] as? String
                        {
                            self.dateTime.append(date)

                        }
                        //if you got null from Json ...
                        if let sp = name["speed"] as? NSNull{
                            //print("got (d)")
                            self.speed.append(0.0)
                        }
                        //simple chk
                        if let speed = name["speed"] as? Double
                        {
                            self.speed.append(speed)

                        }


                        //if you got null from Json ...
                        if let ev = name["reportText"] as? NSNull{
                            //print("got (d)")
                            self.event.append("null")
                        }
                        //simple chk
                        if let event = name["reportText"] as? String
                        {
                            self.event.append(event)

                        }
                        //if you got null from Json ...
                        if let dev = name["route"] as? NSNull{
                            //print("got (d)")
                            self.deviation.append("--")
                        }
                        //simple chk
                        if let devtion = name["route"] as? String
                        {
                            self.deviation.append(devtion)
                        }
                    }
                    self.ActivityIndicator.stopAnimating()
                    dispatch_async(dispatch_get_main_queue()) {
                        self.tableView?.reloadData()
                    }
                }

            }
        } catch let error as NSError {
            print(error.localizedDescription)
        }
    }


    task.resume();
    })
}

最新更新