2016-07-14 84 views
4

我需要滚动到底。我有一个像WhatsApp的聊天应用程序。所以当view出现时table view应该显示最后一行。我很喜欢这条线,并且很好。在的tableview和性能滚动底部

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

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

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

这是Tableview的扩展。

这个解决方案工作正常,在没有太多消息。然后我试图与5000条消息(这样的tableview有5000行,但我分页它们)和键盘出现,当我看到CPU使用率是98-100%。我认为第二个代码是分页问题,​​它会导致将每条消息加载到内存中,并且我的应用程序冻结并接收内存警告。

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

+0

你已经找到了一个解决方案?也使用领域。 – oyalhi

回答

0

如果你有分页,你可以尝试只加载当前页,以及最后一页上,假设你有20条消息中的每一页,在这种情况下,你的表只有40行。然后你可以使用你的函数:

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

}

+0

我使用领域,它有一个自动分页系统。所以我不能这样做。 –

0

试试这个方法:

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) 
      } 
    }) 
+0

不,它是一样的。 –