lastReturn數(shù)據(jù)只會(huì)在下一次進(jìn)入聊天界面的時(shí)候才會(huì)向上移動(dòng)??
方法① : 代碼是用contentSize
的方式寫的 , 就出現(xiàn)了上面的??情況 ;
方法② : 使用scrollToRow:
點(diǎn)擊return之后,數(shù)據(jù)就會(huì)刷新, 數(shù)組就會(huì)多加一個(gè)數(shù)據(jù) , 通過(guò)判斷 (最新數(shù)據(jù)的個(gè)數(shù) -1 ) 就是scrollVIew要跳轉(zhuǎn)的行數(shù)
執(zhí)行跳轉(zhuǎn):
滾動(dòng)到最新消息位置
func scrollToBottom(_ scrollView:UIScrollView,animated:Bool) {
//FIXME: 之前用到contentSize 和 bounds 的高度差來(lái)移動(dòng),最新的數(shù)據(jù)總是會(huì)在下一次才上移
if self.chatMsgs.count == 0 {
return
}
let lastIndexPath = IndexPath(row: self.chatMsgs.count - 1, section: 0)
self.tableView?.scrollToRow(at: lastIndexPath, at: UITableViewScrollPosition.bottom, animated: true)
}