不触摸屏幕就无法查看表格视图项目

Posted

技术标签:

【中文标题】不触摸屏幕就无法查看表格视图项目【英文标题】:Can not see table view items without touching down the screen 【发布时间】:2016-01-28 05:28:23 【问题描述】:

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

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://***.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)
    



【问题讨论】:

你有没有尝试在设置所有 api 响应数据结束时只重新加载一次表? 我该怎么做?我不试试这个 【参考方案1】:

尝试替换此代码:

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


【讨论】:

没什么变化..再次..结果显示在触摸事件中。 我已经编辑了答案。尝试更新代码,因为我已将 api 调用放在后台线程中并在主队列中重新加载表视图数据。

以上是关于不触摸屏幕就无法查看表格视图项目的主要内容,如果未能解决你的问题,请参考以下文章

UITextView应检测链接,否则应传播触摸以查看下面的链接

显示键盘时,表格页脚视图中的 UIButton 不响应触摸

编辑 UITextView 并在表格视图控制器中触摸键盘以外的任何位置时无法关闭键盘

仅将触摸事件转发到被触摸的视图

iPad使用触摸显示弹出框?

触摸时不突出显示表格视图行