禁用滚动到顶部
问题描述:
即时消息建立与Firebase的聊天应用程序,我有一个问题是在reloadData()将滚动顶部。禁用滚动到顶部
我该如何禁用?
这是我的代码
func retrieveDataFromFirebase(){
FIRDatabase.database().reference().child("share").child("post").observe(.value) { (snapshot:FIRDataSnapshot) in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot]{
self.key.removeAll()
self.users.removeAll()
for snap in snapshots{
self.key.append(snap.key)
if let dic = snap.value as? [String:AnyObject]{
let msg = Message()
msg.setValuesForKeys(dic)
self.users.append(msg)
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
答
进入的UIScrollView类(即由UITableView的实现)你有scrollsToTop
变量。
尝试将其设置为false
。
或者您将需要创建一个委托,当您重新加载数据时拒绝滚动到顶部。
参考文献: https://developer.apple.com/reference/uikit/uiscrollview https://developer.apple.com/reference/uikit/uiscrollview/1619421-scrollstotop
https://developer.apple.com/reference/uikit/uiscrollviewdelegate/1619378-scrollviewshouldscrolltotop
答
尝试滚动你的tableview去年指数。
let lastCellIndexPath = NSIndexPath(forRow: yourArray.count-1, inSection: 0)
self.tableView.scrollToRowAtIndexPath(lastCellIndexPath, atScrollPosition: .Bottom, animated: false)
首先,删除DispatchQueue,因为它不需要。其次,在重新加载tableView之前,获取它当前的滚动位置。该属性/值将根据行的高度是否相同而不同,因此有两种方法可以完成。重新加载tableView之后。回滚到那个位置,只要它仍在范围内。请参阅[获取滚动位置](http://stackoverflow.com/questions/2795900/how-can-i-get-the-uitableview-scroll-position-so-i-can-save-it) – Jay