2016-07-14 66 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

不,它是一樣的。 –