ios - UITableViewController 异步查询后消失
<p><p>希望您能帮助我...我已经尝试解决这个问题几天了。</p>
<p>我使用 Parse (www.parse.com) 作为后端,并将其托管在我自己的 AWS 服务器上。</p>
<p><strong>应用的结构</strong>:
在 AppDelegate 中,如果用户已登录,则显示一个 ViewController,它设置了我的 SlideMenuControllerSwift (<a href="https://github.com/dekatotoro/SlideMenuControllerSwift" rel="noreferrer noopener nofollow">https://github.com/dekatotoro/SlideMenuControllerSwift</a>) 和我的 TabBarController。
[ Storyboard]</p>
<p>在我的选项卡栏 Controller 中,我有一个导航 Controller ,当我单击一行时,它会连接到另一个 UITableViewController。</p>
<p><strong>问题</strong>:
<a href="http://imgur.com/izdCBgt" rel="noreferrer noopener nofollow">http://imgur.com/izdCBgt</a> </p>
<p>1) 我点击一行,它对我的解析数据库执行异步查询</p>
<p>2) 数据填充表格然后消失</p>
<p>3) 如果我更改选项卡并返回主选项卡,数据会重新出现</p>
<p>还有</p>
<p>1) 我点击一行,它对我的解析数据库执行异步查询</p>
<p>2) 数据填充表格然后消失</p>
<p>3)如果我回到原来的 UITableViewController,它并不能正常转换回来,我需要来回切换标签,直到它重新出现</p>
<p><strong>代码:</strong></p>
<p>我使用 storyboard segue 连接到文档 TableViewController 。这是我的 DocumentsTableViewController 中的相关代码:</p>
<pre><code>class DocumentTableViewController: UITableViewController, UIDocumentInteractionControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MMCropDelegate {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
print("initDocs")
}
var specificDocs=(){
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!
// progressHUD.hideAnimated(true)
// } catch {
// print("error!")
// }
// FIND WHY THIS DOESNT WORK....WHAT THE F
query.findObjectsInBackgroundWithBlock {
(objects: ?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
self.specificDocs = objects as!
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.documentCategory!.type!
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
// print(UIApplication.sharedApplication().keyWindow?.performSelector("recursiveDescription"))
return cell!
}
deinit {
print("docs deinit")
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>哦,现在我看到了您的代码的意图。但是,你这样做有什么理由吗?直接调用loadView()是不对的。</p>
<pre><code>tableView.dataSource = self
tableView.delegate = self
</code></pre>
<p>在 viewWillAppear 中移到第一个,除了完成速度太快而无法从解析中接收之外,它将起作用。</p>
<blockquote>
<p>Normal way will be like below:</p>
</blockquote>
<pre><code>var specificDocs=(){
didSet{
dispatch_async(dispatch_get_main_queue(), {
//we might be called from the parse block which executes in seperate thread
self.loadView()
})
}
</code></pre>
<blockquote>
<p>self.loadView() to self.tableView.reloadData().</p>
</blockquote>
<p><code>tableView.dataSource = self</code><br/>
<code>tableView.delegate = self</code></p>
<blockquote>
<p>That doesn't needs.</p>
</blockquote>
<pre><code>override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tableView.reloadData()}
</code></pre>
<blockquote>
<p>That doesn't needs too.</p>
</blockquote>
<p><strong>无论如何,您的代码只需将调用 self.loadView() 修改为 self.tableView.reloadData()</strong></p></p>
<p style="font-size: 20px;">关于ios - UITableViewController 异步查询后消失,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38937581/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38937581/
</a>
</p>
页:
[1]