ios - 异步查询后 UITableViewController 消失

标签 ios iphone swift uitableview parse-platform

希望你能帮我解决问题...我已经尝试解决这个问题好几天了。

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

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

在我的标签栏 Controller 中,我有一个导航 Controller ,它指向一个 UITableViewController,当我单击一行时,它会转到另一个 UITableViewController。

问题: http://imgur.com/izdCBgt

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

2) 数据填充表然后消失

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

还有

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

2) 数据填充表然后消失

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

代码:

我使用 storyboard segue 转至文档 TableView Controller 。这是我的 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")
    }
}

最佳答案

哦,现在我明白你代码的意图了。但是,你这样做有什么理由吗?直接调用 loadView() 是不正确的。

tableView.dataSource = self
tableView.delegate = self

移动到 viewWillAppear 中的第一个,除了完成得太快无法从解析中接收外,它将起作用。

Normal way will be like below:

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() to self.tableView.reloadData().

tableView.dataSource = self
tableView.delegate = self

That doesn't needs.

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

That doesn't needs too.

无论如何,您的代码只需将调用 self.loadView() 修改为 self.tableView.reloadData() 即可

关于ios - 异步查询后 UITableViewController 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38937581/

相关文章:

ios - 动态更新约束不起作用

javascript - 一两秒钟后视频变黑

ios - 如何在iOS中使用动态按钮上的标签?

iphone - CGPDF 函数与 iBooks PDF 渲染

ios - 在点击的链接上缩放网页

swift - 有条件地在同一 View 上设置两个不同的过渡

ios - swift - 绘制曲线时出错

iOS:模态 viewController 如何记住它的属性?

ios - 将数据从 SettingsView Controller 传递到 Mainstoryboard - Swift

ios - NSOperationQueue 的 addOperation : operation is finished and cannot be enqueued?