在做聊天界面時(shí), 如果發(fā)送的內(nèi)容中有圖片加文字,使用
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.dataArray.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: isAnimated)
和
self.tableView.setContentOffset(<#T##contentOffset: CGPoint##CGPoint#>, animated: <#T##Bool#>)
這兩個(gè)方法有可能滾動(dòng)不到 底部,會(huì)有部分沒有滾動(dòng)完
解決方法如下
var isFirstComing = 0
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
var cellHeight: CGFloat = 0.0
if indexPath.row <= dataArray.count - 1 && isFirstComing < 2 {
if indexPath.row == dataArray.count - 1 {
isFirstRefresh += 1
}
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.dataArray.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: isAnimated)
}
return cellHeight
}
并且在viewDidAppear中在刷新一次
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
if self.dataArray.count > 1 {
self.showEmailTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.dataArray.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: isAnimated)
}
}
注意的是,這里要判斷是否是第一次進(jìn)入頁(yè)面, isFirstComing為<2 時(shí) tableView.reloadData 方法執(zhí)行2次后 停止