ios - Swift:如何无延迟地刷新 tableview (UIRefreshControl)

标签 ios json swift uitableview

使用 PHP 和 JSON 到 Swift 3,用 MYSQL 数据库中的对象填充我的 TableView 。我有一个下拉刷新功能,但是当我下拉刷新时,它会在中途滞后一秒钟然后继续(比如轮子不会旋转一秒钟)。

我怎样才能更流畅地更新我的 tableview,因为我猜测随着我在未来向数据库中添加更多的内容,延迟就越大。我的数据库中目前有 12 个对象,所以想象一下有 100 多个对象吧。

在viewDidLoad中

// Pull to Refresh
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(handleRefresh), for: .valueChanged)
    if #available(iOS 10.0, *) {
        myTableView.refreshControl = refreshControl
        print("iOS 10")
    } else {
        myTableView.addSubview(refreshControl)
        print("iOS 9 or iOS 8")
    }

然后拉动刷新函数

// Pull to Refresh
func handleRefresh(refreshControl: UIRefreshControl) {

    // Fetching Data for TableView
    retrieveDataFromServer()

    // Stop Refreshing
    refreshControl.endRefreshing()
}

// Retrieving Data from Server
func retrieveDataFromServer() {

    // Loading Data from File Manager
    loadData()

    let getDataURL = "http://example.com/receiving.php"
    let url: NSURL = NSURL(string: getDataURL)!

    do {
        let data: Data = try Data(contentsOf: url as URL)
        let jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray

        // Clear the arrays
        self.followedArray = [Blog]()
        self.mainArray = [Blog]()

        // Looping through jsonArray
        for jsonObject in jsonArray {

            if let blog = Blog(jsonObject:jsonObject as! [String : Any]) {

                // Check if Identifiers Match
                if followedIdentifiers.contains(blog.blogID) {
                    self.followedArray.append(blog)
                } else {
                    self.mainArray.append(blog)
                }
            }
        }
    } catch {
        print("Error: (Retrieving Data)")
    }
    myTableView.reloadData()
}

最佳答案

引用苹果示例代码在以下位置:

http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html

一些建议:

不要在 cellForRowAtIndexPath: 方法中显示数据,因为此时单元格尚未显示。尝试在 UITableView 的委托(delegate)中使用 tableView:willDisplayCell:forRowAtIndexPath: 方法。

重复使用单元格/页眉/页脚的单个实例,即使您需要显示更多内容。

如果需要任何具体信息,请告诉我。

关于ios - Swift:如何无延迟地刷新 tableview (UIRefreshControl),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44939311/

相关文章:

ios - 启用 Facebook 重大更改 2013 年 2 月 : login Does Not Work

swift - 代码 : Using custom fonts inside Dynamic framework

swift - 如何将字符串转换为 UIColor (Swift)

ios - UITableView 右侧的单元格空间

ios - 对 Firebase 数据库的多次调用是否按调用顺序返回?

ios - 在改变它们的同时传递 JSON 对象

java - JSONObject 使用单例

javascript - 使用 d3.json 事件处理程序时如何正确控制上下文

ios - swift : Player audio file from another class file

ios - CFRelease中的空指针(由静态分析器告知)