菜鸟教程小白 发表于 2022-12-11 19:34:14

ios - 刷新tableView刷新数据时闪烁


                                            <p><p>因此,由于索引超出范围问题,刷新功能不断使我的应用程序崩溃。然后我在第一次清空我的列表后添加了 reloadData。现在我在刷新时得到这个闪烁,因为它首先加载空列表然后加载填充列表。我应该在哪里清空列表或如何摆脱闪烁?我正在使用 swift 3 和 Xcode 8.2.1</p>

<pre><code>func getData()
{
    fetchedUsers.removeAll()
    self.userTableView.reloadData()

    //myphpfileurl
    var request = URLRequest(url: URL(string: url)!)
    request.httpMethod = &#34;GET&#34;

    let configuration = URLSessionConfiguration.default
    let session = URLSession(configuration: configuration, delegate: nil, delegateQueue: OperationQueue.main)

    let task = session.dataTask(with: request) { (data, response, error) in

      if (error != nil) {
            print(&#34;Error&#34;)
      }
      else{
            do{
                let fetchedData = try JSONSerialization.jsonObject(with: data!, options: .mutableLeaves) as! NSArray

                for eachFetchedUsers in fetchedData {
                  let eachUsers = eachFetchedUsers as!
                  let id = eachUsers[&#34;id&#34;] as! String
                  let name = eachUsers[&#34;Nimi&#34;] as! String
                  let email = eachUsers[&#34;Sahkoposti&#34;] as! String
                  let company = eachUsers[&#34;Yritys&#34;] as! String

                  self.fetchedUsers.append(Users(id : id, name: name, email : email, company: company))

                }
                self.userTableView.reloadData()
                self.refreshControl.endRefreshing()
            }
            catch{
                print(&#34;Error 2&#34;)
            }
      }
    }
    task.resume()
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>像这样尝试重新加载表格</p>

<pre><code>DispatchQueue.main.async {
      self.userTableView.reloadData()
      self.refreshControl.endRefreshing()
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 刷新tableView刷新数据时闪烁,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43608034/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43608034/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 刷新tableView刷新数据时闪烁