【发布时间】:2016-05-27 17:36:53
【问题描述】:
在聊天应用程序中,我在chat table view 上实现Load More 按钮。
当用户点击加载更多按钮时,旧消息(以及屏幕上显示的所有当前消息)会从数据库中获取(使用 NSFetchedResultsController),然后我在 chat table view 上调用 reloadData() 方法。
我想在重新加载表格之前将tableview的滚动位置设置为顶行。
根据消息文本,所有消息单元格的高度动态。
我在故事板上使用auto layout。
我的代码:
let oldContentheight = _chatTableView.contentSize.height
_chatTableView.reloadData()
executeOnMain {
if self.firstTimeFetching {
self.firstTimeFetching = false
self.scrollTableToLastRow(withAnimation: false)
//this is for going to last cell when we come to chat screen
}
else {
//after pressing load more button
let newContentHeight = self._chatTableView.contentSize.height
let newOffset = CGPointMake(0, newContentHeight - oldContentheight)
self._chatTableView.setContentOffset(newOffset, animated: false)
}
}
【问题讨论】:
标签: ios swift uitableview