简体   繁体   English

Swift-UITableView滚动到底部很慢

[英]Swift - UITableView scroll to bottom is slow

Problem 问题

scrolling to the bottom of table view is slow the table view contains about 25 items and there is a bit of complexity in creating each cell so creating 25 of them at the same time takes about 200 ms(believe me there is no way I can make it any more optimized) 滚动到表格视图的底部很慢,表格视图包含大约25个项目,创建每个单元格有点复杂,因此,同时创建25个单元格大约需要200毫秒(相信我没有办法进一步优化)

Soloutions I've tried 我尝试过的解答

there are basically two options for scrolling to bottom of table view(And I have searched the entire stack over flow for this) 基本上有两个选项可滚动到表格视图的底部(为此,我在整个流程中搜索了整个堆栈)

  1. tableView.scrollRectToVisible OR tableView.setContentOffset tableView.scrollRectToVisibletableView.setContentOffset
  2. tableView.scrollToRowAtIndexPath

the problem with the first option is, although it is pretty fast and it doesn't recreate every cell in the way from top to bottom of tableView, I need to call a new main queue to get it to work. 第一个选项的问题是,尽管它非常快并且不会以tableView的顶部到底部的方式重新创建每个单元格,但我需要调用一个新的主队列来使其工作。 if I call it in the same queue as the one that called tableView.reloadData() , then there will be a strange lag when scrolling the tableview(logs show that lots of cells are being re created unnecessarily, and I mean lots of them). 如果我在与调用tableView.reloadData()队列相同的队列中调用它,那么在滚动tableview时会有一个奇怪的延迟(日志显示,不必要地重新创建了许多单元格,我的意思是很多单元格) 。 and let me add that both threads are main thread. 让我补充一点,这两个线程都是主线程。 but still I must call the dispatch_get_main_queue() to get rid of that awkward lag. 但我仍然必须调用dispatch_get_main_queue()摆脱这种尴尬的滞后。 however new main thread adds a good delay to the initial load. 但是,新的主线程会增加初始加载的延迟。
here is my code: 这是我的代码:

func loadData(groupID: String){
    self._chats = self._cache[groupID]!
    _tableView.reloadData()
    dispatch_async(dispatch_get_main_queue()) {
        let rect = CGRect(x: 0, y: self._tableView.contentSize.height - self._tableView.bounds.height, width: self._tableView.contentSize.width, height: self._tableView.bounds.height)
        self._tableView.scrollRectToVisible(rect, animated: false)
    }
}

again if I don't use the dispatch_get_main_queue() the tableView would be supper laggy. 再次,如果我不使用dispatch_get_main_queue(),tableView将晚饭。 and if I do use it, then there will be about 100 ms delay in execution which makes the initial loading slow. 如果我确实使用了它,那么执行将有大约100毫秒的延迟,这会使初始加载变慢。

the second option is slow too, cause scrolling to the last indexPath means creating every cell in the way. 第二个选项也很慢,导致滚动到最后一个indexPath意味着以这种方式创建每个单元格。 and that cannot happen in the background and doing it in the main thread means freezing the UI for about 200 ms. 而这不可能在后台发生,而在主线程中执行则意味着将UI冻结大约200毫秒。

can you suggest any other solution or find something wrong with what I'm doing? 您可以提出其他解决方案或发现我的工作有问题吗? I've tried every thing and still tableView isn't fast enough. 我已经尝试了所有事情,但tableView仍然不够快。

您可以使用Xcode Instruments之一的Time Profiler来了解您的应用堆栈在哪个进程上。

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

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