2016-08-13 48 views
2

希望你能帮助我......我一直在试图解决这个问题几天了。UITableViewController在异步查询后消失

我使用Parse(www.parse.com)作为我的后端,并将它托管在我自己的AWS服务器上。应用的

结构: 在AppDelegate中,如果用户已登录,显示视图控制器,设置了我的SlideMenuControllerSwift(https://github.com/dekatotoro/SlideMenuControllerSwift)和我的TabBarController。在我的标签栏控制器中,我有一个导航控制器,导致UITableViewController,当我点击一行时跳入另一个UITableViewController。

问题http://imgur.com/izdCBgt

1)I点击一个行,并将其执行非同步查询我的解析数据库

2)数据填充表,然后消失

3 )如果我更改标签并返回到主选项卡,数据会再次出现

1)我点击一行,它执行非同步查询到我的解析数据库

2)数据填充表,然后消失

3)如果我回到原来的UITableViewController,它不转换回正确的,我需要改变标签来回,直到它再次

代码:

我Segue公司用故事板SEGUE文件表视图控制器。这里是我的DocumentsTableViewController的相关代码:

class DocumentTableViewController: UITableViewController, UIDocumentInteractionControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MMCropDelegate { 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    print("initDocs") 
} 

var specificDocs=[Document](){ 
    didSet{ 
     dispatch_async(dispatch_get_main_queue(), { 
      //we might be called from the parse block which executes in seperate thread 
      self.loadView() 
     }) 
    } 
} 


override func viewDidLoad() { 
    tableView.dataSource = self 
    tableView.delegate = self 

    print("we loadeD") 
    super.viewDidLoad() 

    navigationItem.title = "Job Documents" 

    let cameraButton = UIBarButtonItem(image: UIImage(named: "Camera"), style: .Plain, target: self, action: #selector(self.didPressCamera(_:))) 
    navigationItem.rightBarButtonItem = cameraButton 

    self.queryForTable() 

} 

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 
    self.tableView.reloadData() 
} 


// Define the query that will provide the data for the table view 
func queryForTable() { 
    // Run a spinner to show a task in progress 
    let progressHUD = MBProgressHUD.showHUDAddedTo(self.view, animated: true) 
    progressHUD.label.text = "Loading..." 

    //2 using those jobActions to find which documents are mine 
    let query = PFQuery(className: Document.parseClassName()) 
    query.whereKey(Document.jobActionCol(), containedIn: jobActions) 
    query.includeKey(Document.documentCategoryCol()) 
//  do { 
//   let test = try query.findObjects() 
//   self.specificDocs = test as! [Document] 
//   progressHUD.hideAnimated(true) 
//  } catch { 
//   print("error!") 
//  } 
// FIND WHY THIS DOESNT WORK....WHAT THE F 
     query.findObjectsInBackgroundWithBlock { 
      (objects: [PFObject]?, error: NSError?) -> Void in 
      if error == nil { 
       // The find succeeded. 
       self.specificDocs = objects as! [Document] 
       print("done") 
       progressHUD.hideAnimated(true) 

      } else { 
       // Log details of the failure 
       print("Error: \(error!) \(error!.userInfo)") 
      } 
     } 
    } 


    // MARK: - Table view data source 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return specificDocs.count 

    } 

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") 
     if ((cell) == nil){ 
      cell = UITableViewCell.init(style: .Default, reuseIdentifier: "cellIdentifier") 
     } 

     cell!.textLabel?.text = specificDocs[indexPath.row].documentCategory!.type! 
     print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") 
//  print(UIApplication.sharedApplication().keyWindow?.performSelector("recursiveDescription")) 
     return cell! 
    } 


    deinit { 
     print("docs deinit") 
    } 
} 
+0

因为我不能把这个原来的职位。下面是我的故事板的截图:http://imgur.com/Hntac8L – Rioner

+0

你是否在后台线程中的某处调用了'tableView.reloadData()? – FelixSFD

+0

不,只有在指定Docs更新时才调用主线程上的loadView,以及何时调用reloadData()中的viewWillAppear – Rioner

回答

0

哦,现在我看到你的代码的意图。但是,你有什么理由吗?这是不正确的方式,直接调用loadView()。

tableView.dataSource = self 
tableView.delegate = self 

移动到第一个viewWillAppear和,它将工作,除非太快从接收解析。

正常的方式将是象下面这样:

var specificDocs=[Document](){ 
didSet{ 
    dispatch_async(dispatch_get_main_queue(), { 
     //we might be called from the parse block which executes in seperate thread 
     self.loadView() 
    }) 
} 

self.loadView()来self.tableView.reloadData()。

tableView.dataSource = self
tableView.delegate = self

这并不需要。

override func viewWillAppear(animated: Bool) { 
super.viewWillAppear(animated) 
self.tableView.reloadData()} 

这并不需要太多。

无论如何,你的代码将工作只是修改调用self.loadView()来self.tableView.reloadData()

+0

谢谢Jay Choi!我不敢相信这么愚蠢的事情会让我头痛不已,我正在调试比我需要的更多的东西。 – Rioner