滚动到 TableView 的最后一个元素需要太多时间 [英] Scroll to last element of TableView take too much time

查看:20
本文介绍了滚动到 TableView 的最后一个元素需要太多时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟 Whatsapp 聊天,任何单元格都会有一个图像(用于气泡的尾部),一个气泡,它只是带有颜色和一些圆角半径的视图,以及一个表示消息文本的标签.

I'm trying to simulate a Whatsapp Chat any cell will have an image (for tail of the bubble), a bubble which is just View with color and some corner radius and a label which will represent the text of the message.

我在通话前后都印了一张照片

I've put a print before and after the call

self.messagesTableView.reloadData()

一旦后打印被称为 tableView 保持一些时间 doint 我不知道在显示数据之前是什么.在索引路径插入行也会发生同样的情况,需要一些时间才能显示插入动画.

Once the after print is called tableView keeps some time doint I don't know what till the data is shown. And same happens with Insert row at indexpath, it takes some time till show the insert animation.

func displayMessages(viewModel: GroupChatMessages.GetChatMessages.ViewModel) {
    let displayedMessage = viewModel.displayedMessages

    print ("i'm here!")

    messages = displayedMessage!

    //self.messagesTableView.performSelectorOnMainThread(Selector("reloadData"), withObject: nil, waitUntilDone: true)

    self.messagesTableView.reloadData()

    print ("i'm here2!")

    firstTime = false
    self.setVisible(hiddenTableView: false, hiddenChatLoader: true)
    self.scrollToLastMessage(false)
    self.messagesLoaded = true

}

我尝试使用队列和 reloadData() 之前的注释行进行调度,但没有任何效果,也没有任何表示重要的时间.

I've tried to do dispatched with queue, and the commented line before reloadData(), but nothings works and nothing represent a significative time.

也许是为了泡泡的形象?我不知道.我将此图片保存在 Assets 中,因此我不会从互联网上下载.

Maybe could be for the image of the bubble? I don't know. I have this image saved on Assets, so I'm not downloading it from internet.

self.setVisible 只是隐藏加载程序并显示 tableView,但我也尝试将其向上移动并且没有任何变化.您需要任何进一步的信息,请告诉我.谢谢!

self.setVisible just hide the loader and show the tableView but I've tried too moving it up and nothings changes. Any further information you need let me know. Thanks!

嗯,我已经看到问题出在滚动到最后一个单元格上,这是它占用大部分时间的地方.

Well I've seen that the problem comes from the scroll to last cell, this is where it takes the major part of the time.

 func scrollToLastMessage(animated: Bool) {
    let section = 0
    let lastItemIndex = self.messagesTableView.numberOfRowsInSection(section) - 1
    let indexPath:NSIndexPath = NSIndexPath.init(forItem: lastItemIndex, inSection: section)
    self.messagesTableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: animated)
    self.scrollDownButton.hidden = true
}

有一种可能性可以优化该滚动,因为我必须进行滚动,因为一旦加载数据,我看到的第一个是 tableView 的顶行,但我想看到底部的(最后的).谢谢!

There is a posibility to optimize that scroll, because I have to do a Scroll because once the data is loaded, the first I've see is the top row of the tableView, but I would like to see the bottom one (last). Thanks!

推荐答案

最后,我发现可以避免在等待滚动到最后一个元素时死亡的解决方案 是交换表格的方向

Finally the solution that I've found to avoid dying while waiting scrolling to last element any single time, is swapping orientation of table

tableView.transform = CGAffineTransformMakeRotation(-M_PI);cell.transform = CGAffineTransformMakeRotation(M_PI);

现在headerView 和footerView 颠倒了.例如,如果您想使用此配置在 TableView 底部(视觉上)插入行,您应该将其添加到位置 0 forRow: 0 atSection: "WhereYouAre".这样当你添加新元素时,不需要滚动,因为滚动是自动的.恕我直言,惊人而奇怪的答案.

Now headerView and footerView are reversed. For exemple, if you would like insert rows at (visually) at the bottom of the TableView with this configuration you should add it at position 0 forRow: 0 atSection: "WhereYouAre". This way when you add new element, no scroll is needed, because scroll is automatically. Amazing and strange answer IMHO.

我在这里找到了这个解决方案:

I've found this solution here:

解决方案链接

@Christos Hadjikyriacou 在那里解决了.

@Christos Hadjikyriacou solved there.

这篇关于滚动到 TableView 的最后一个元素需要太多时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆