简体   繁体   English

刷新时,UITableView无法滚动

[英]UITableView can't scroll while refreshing

Using Parse I am downloading images, text etc. and using them in table view cells. 使用解析,我正在下载图像,文本等,并在表格视图单元格中使用它们。 However, while refreshing (when the indicator is animating in the navigation bar) I cannot scroll or move on the app. 但是,在刷新时(当导航栏中的指示器处于动画状态时),我无法在应用程序上滚动或移动。 It is as if the app has become unresponsive until the table view is done refreshing. 好像应用程序变得无响应一样,直到表视图完成刷新为止。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Like some of the comments already said, it's due to what's called "blocking the main thread". 就像已经说过的一些评论一样,这是由于所谓的“阻塞主线程”。 See this great tutorial to learn more about concurrency in Swift. 请参阅教程,以了解有关Swift中并发的更多信息。 The WithBlock or InBackground methods that you call on your Parse query should call it asynchronously. 您在Parse查询中调用的WithBlockInBackground方法应异步调用它。 If for some reason you still have the issue, you should nest your Parse query inside a dispatch_async block. 如果由于某些原因仍然存在问题,则应将Parse查询嵌套在dispatch_async块中。 Here's a useful tutorial on how to do this. 这是有关如何执行此操作的有用教程。

Caution: Always do UI updating on your main thread. 注意: 始终线程上进行UI更新。 Bad things will happen if you don't. 如果不这样做,不好的事情将会发生。

Here is a simple example on how to dispatch to a different thread, download the image and get back to main thread when the download is done. 这是一个有关如何分派到其他线程,下载映像并在完成下载后返回主线程的简单示例。

func downloadImageAndUpdateUI(){
    imageView?.image = nil

    if let imageUrl = aValidUrl{
        //get image on another thread
        let qos = Int(QOS_CLASS_BACKGROUND.rawValue)
        dispatch_async(dispatch_get_global_queue(qos, 0), {
            let imageData = NSData(contentsOfURL: imageUrl)
            //get back to main thread
            dispatch_async(dispatch_get_main_queue(), {
                if imageUrl == self.requestedImageUrl { //making sure if this image is still needed (or if user havent moved on to something else)
                    if imageData != nil{
                        self.imageView.image = UIImage(data: imageData!)
                    }
                }
            })
        })
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM