菜鸟教程小白 发表于 2022-12-11 17:15:19

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(&#34;initDocs&#34;)
}

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(&#34;we loadeD&#34;)
    super.viewDidLoad()

    navigationItem.title = &#34;Job Documents&#34;

    let cameraButton = UIBarButtonItem(image: UIImage(named: &#34;Camera&#34;), 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 = &#34;Loading...&#34;

    //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(&#34;error!&#34;)
//      }
// FIND WHY THIS DOESNT WORK....WHAT THE F
      query.findObjectsInBackgroundWithBlock {
            (objects: ?, error: NSError?) -&gt; Void in
            if error == nil {
                // The find succeeded.
                self.specificDocs = objects as!
                print(&#34;done&#34;)
                progressHUD.hideAnimated(true)

            } else {
               // Log details of the failure
                print(&#34;Error: \(error!) \(error!.userInfo)&#34;)
            }
      }
    }


    // MARK: - Table view data source

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

    }

    override func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
      return 1
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {

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

      cell!.textLabel?.text = specificDocs.documentCategory!.type!
      print(&#34;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!&#34;)
//      print(UIApplication.sharedApplication().keyWindow?.performSelector(&#34;recursiveDescription&#34;))
      return cell!
    }


    deinit {
      print(&#34;docs deinit&#34;)
    }
}
</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&#39;t needs.</p>
</blockquote>

<pre><code>override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tableView.reloadData()}
</code></pre>

<blockquote>
<p>That doesn&#39;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]
查看完整版本: ios - UITableViewController 异步查询后消失