【问题标题】:UITableViewController disappears after asynchronous queryUITableViewController 异步查询后消失
【发布时间】:2016-08-13 22:40:05
【问题描述】:

希望您能帮助我...我这几天一直在尝试解决这个问题。

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

应用的结构: 在 AppDelegate 中,如果用户已登录,则显示一个 ViewController,它设置了我的 SlideMenuControllerSwift (https://github.com/dekatotoro/SlideMenuControllerSwift) 和我的 TabBarController。 [故事板][1]

在我的标签栏控制器中,我有一个导航控制器,当我点击一行时,它会连接到另一个 UITableViewController。

问题http://imgur.com/izdCBgt

1) 我点击一行,它对我的​​解析数据库执行异步查询

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

3) 如果我更改选项卡并返回主选项卡,数据会重新出现

还有

1) 我点击一行,它对我的​​解析数据库执行异步查询

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

3)如果我回到原来的 UITableViewController,它并不能正常转换回来,我需要来回切换标签,直到它重新出现

代码:

我使用 storyboard 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")
    }
}

【问题讨论】:

  • 因为我不能把这个放在原来的帖子里。这是我的故事板的截图:imgur.com/Hntac8L
  • 你是否在后台线程中的某个地方调用tableView.reloadData()
  • 不行,只有在指定Docs更新时才在主线程调用loadView,在viewWillAppear中调用reloadData()时

标签: ios iphone swift uitableview parse-platform


【解决方案1】:

哦,现在我看到了您的代码的意图。但是,你这样做有什么理由吗?直接调用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()

【讨论】:

  • 谢谢蔡杰!我不敢相信这么愚蠢的事情让我这么头疼……我调试的东西比我需要的要多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 2020-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
相关资源
最近更新 更多