UITableView deleterows滚动到顶部 [英] UITableView deleterows scroll to top
问题描述
我做了一个查询来删除UITableView中的指定行,但当deleteRows被调用UITableView滚动到顶部时,如何防止它滚动到顶部?为什么滚动到顶部!
我尝试调用一个方法来强制它滚动到下来,但tableview滚动到顶部之后返回
删除的代码
FIRDatabase.database()。reference()。child(messages)。child(self.roomId).observe .childRemoved:{(snap)in
print(child Removed)
for self.messageArray {
let message:NSDictionary = msg as!NSDictionary
messageContent:[String:Any] = message as![String:Any]
如果messageContent [messageID] as!String == snap.key {$ b $ let indexRemoved = self.messageArray.index :msg)
self.messageArray.remove(msg)
self.userArray.removeObject(at:indexRemoved)
let indexPath = IndexPath(row:indexRemoved,section:0)
print(indexPath)
self.conversationTableView.deleteRows(at:[indexPath],with:UITabl eViewRowAnimation.none)
break;
})
首先在做任何事情之前,抓住第一个可见项目的当前滚动位置(
let savedIndex = myTableView.indexPathsForVisibleRows?.first
$ c $然后,一旦行从数组中删除,你重新加载tableview,你可以返回到它所在的位置
$ b $ p
b
myTableView.scrollToRowAtIndexPath(savedIndex,atScrollPosition:.Top,animated:false)
如果你需要另外一个选项(变量高度),你可以使用
$ $ p code> tableView。 contentOffset.y;
另外,不需要迭代整个数组来查找要删除的行。假设Firebase节点数据以类的形式存储在数组中,那么在Firebase键作为每个类的属性的数组中,可以简单地获取数组中该元素的索引并将其删除。这里有一个选项:
let key = snapshot.key
$ b $
如果let index = self.myArray.indexOf ({$ 0.firebaseKey == key}){
self.myArray.removeAtIndex(index)
self.myTableView.reloadData()
}
这个对象看起来像:
class MyClass {
var firebaseKey:String?
var some_var:String?
var another_var:String?
}
I made a query to delete specified rows in UITableView but the problem when the deleteRows is called the UITableView scroll to top so how to prevent it scrolling to top? and why it scroll to top!
I try called a method to force it scroll to down but the tableview scroll to top first after return the code of remove
FIRDatabase.database().reference().child("messages").child(self.roomId).observe(.childRemoved, with: {(snap) in print("child Removed") for msg in self.messageArray { let message: NSDictionary = msg as! NSDictionary let messageContent: [String: Any] = message as! [String: Any] if messageContent["messageID"] as! String == snap.key { let indexRemoved = self.messageArray.index(of: msg) self.messageArray.remove(msg) self.userArray.removeObject(at: indexRemoved) let indexPath = IndexPath(row: indexRemoved, section: 0) print(indexPath) self.conversationTableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.none) break; } } })
解决方案There are a few issues with the code that can be easily corrected.
First before doing anything, grab the current scroll position of the first visible item (given the cells are the same size)
let savedIndex = myTableView.indexPathsForVisibleRows?.first
then once the row is deleted from the array and you reload the tableview, you can return to where it was with
myTableView.scrollToRowAtIndexPath(savedIndex, atScrollPosition: .Top, animated: false)
If you need another option (variable heights), you can use
tableView.contentOffset.y;
Also, there's no need to iterate over the entire array to find the row to delete from it. Assuming the Firebase nodes(s) data are stored in the array as a class, in the array with the Firebase key as a property of each class, you can simply get the index of that element in the array and remove it. Here's an option
let key = snapshot.key if let index = self.myArray.indexOf({$0.firebaseKey == key}) { self.myArray.removeAtIndex(index) self.myTableView.reloadData() }
This object would look like:
class MyClass { var firebaseKey: String? var some_var: String? var another_var: String? }
这篇关于UITableView deleterows滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!