不触摸屏幕就无法看到 table 查看项目
Can not see table view items without touching down the screen
我的 tableView class 我已经从我的服务器获取了数据并且可以在 table View 中显示,但问题是当我完全获取来自服务器的数据在我按下屏幕之前从未显示在 table 视图中。当我按下屏幕时,我可以看到我的数据,但我想在获得数据后查看我的数据。这是我的class
..
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...
//
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();
})
}
我的 tableView class 我已经从我的服务器获取了数据并且可以在 table View 中显示,但问题是当我完全获取来自服务器的数据在我按下屏幕之前从未显示在 table 视图中。当我按下屏幕时,我可以看到我的数据,但我想在获得数据后查看我的数据。这是我的class ..
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...
//
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();
})
}