在tableview和性能中滚动到底 [英] Scrolling bottom in tableview and performance

查看:184
本文介绍了在tableview和性能中滚动到底的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要滚动到底部。我有一个像whatsapp的聊天应用程序。因此,当视图出现时,表视图应显示最后一行。我用下面的行来实现这个并且工作得很好。

I need to scroll to bottom. I have a chat app like whatsapp. So when view appears table view should show last row. I am achiving this with following line and works nice.

tableView.setContentOffset(CGPointMake(0, CGFloat.max), animated: false)

我还需要在键盘出现时滚动到底部。我正在使用自动布局,上面的行不起作用。为了做到这一点,我使用以下行:

Also I need to scroll to bottom when keyboard appears. I am using Auto Layout and above line is not working. For to do this i am using following line:

func scrollToLastRow(animated: Bool) {
    if self.numberOfRowsInSection(0) > 0 {
        self.scrollToRowAtIndexPath(NSIndexPath(forRow: self.numberOfRowsInSection(0) - 1, inSection: 0), atScrollPosition: .Bottom, animated: animated)
    }
}

这是Tableview的扩展。

This is a extension for Tableview.

当没有太多消息时,此解决方案正常工作。然后我尝试了5000条消息(因此tableview有5000行,但我正在分页)当键盘出现时,我看到cpu使用率为%98-100。我认为第二个代码是分页问题,​​它会导致每个邮件加载到ram并且我的应用程序冻结并接收ram警告。

This solution is working fine when there is no too much message. Then I tried with 5000 messages (so tableview have 5000 rows, but i am paging them) And when keyboard appears i see cpu usage is %98-100. I think the second code is problem for pagination, it causes loading every message to ram and my app freezes and receiving ram warning.

如何滚动到底部而没有任何性能问题?

How to scroll to bottom without any performance issue?

推荐答案

尝试这种方法:

let delay = 0.1 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

     dispatch_after(time, dispatch_get_main_queue(), {

          let numberOfSections = self.tableView.numberOfSections
          let numberOfRows = self.tableView.numberOfRowsInSection(numberOfSections-1)
            if numberOfRows > 0 {
                let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
                self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
            }  
     })

这篇关于在tableview和性能中滚动到底的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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