如何在不滚动表格的情况下将行插入到 UITableView 的顶部?就像当您拖动以重新加载推特上的新推文或 whatsApp 中的消息时一样
我尝试执行 begin/endUpdate 的另一部分,要求它滚动到第 3 个索引,但没有成功。它总是滚动到 tableView 的顶部。我希望它保持在添加新行之前的相同位置。
self.tableView.beginUpdates()
let indexPath = IndexPath(row: 3, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
self.tableView.endUpdates()
附言这是我的真实项目中的示例项目,单元格的大小根据消息大小而变化。
这是项目的链接: https://github.com/akawther/TableView
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var loadMore: UIRefreshControl!
var labels = ["A","B","C","D","E","F","G","H","I","J","K"]
override func viewDidLoad() {
super.viewDidLoad()
self.loadMore = UIRefreshControl()
self.loadMore.attributedTitle = NSAttributedString(string: "Pull to reload more")
self.loadMore.addTarget(self, action: #selector(ViewController.loadMoreChats), for: .valueChanged)
self.tableView.addSubview(self.loadMore)
// Do any additional setup after loading the view, typically from a nib.
}
func loadMoreChats() {
DispatchQueue.main.async(execute: {() -> Void in
self.tableView.beginUpdates()
self.labels.insert("3", at: 0)
self.labels.insert("2", at: 0)
self.labels.insert("1", at: 0)
let indexPaths = [
IndexPath(row: 0, section: 0),
IndexPath(row: 1, section: 0),
IndexPath(row: 2, section: 0),
]
self.tableView.insertRows(at: indexPaths, with: .top)
self.tableView.endUpdates()
self.loadMore.endRefreshing()
self.tableView.beginUpdates()
let indexPath = IndexPath(row: 3, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
self.tableView.endUpdates()
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return labels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! customCellTableViewCell
cell.label.text = labels[indexPath.row]
return cell
}
}
更新#1: 我刚刚也尝试过用 CATransaction 包围我的第二次 being/end 更新,但显示的顶部仍然是 1 而不是我想要的 A:
CATransaction.begin()
self.tableView.beginUpdates()
CATransaction.setCompletionBlock { () -> Void in
let indexPath = IndexPath(row: 3, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
}
self.tableView.endUpdates()
CATransaction.commit()
更新#2: github 项目更新了@beyowulf 的回答
最佳答案
当 loadMoreChats
是按钮的目标时,这段代码对我来说工作正常:
func loadMoreChats() {
self.loadMore.endRefreshing()
self.labels.insert("3", at: 0)
self.labels.insert("2", at: 0)
self.labels.insert("1", at: 0)
self.tableView.reloadData()
let indexPath = IndexPath(row: 3, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
}
使用刷新控件会使 scrollToRow
被触发它的平移手势覆盖。
更新
请注意,您引用的应用程序不会以这种方式使用刷新 Controller ,但如果您坚持使用刷新 Controller ,我认为您可以在以编程方式滚动表格 View 时暂时禁用平移手势。像这样的东西:
func loadMoreChats() {
self.loadMore.endRefreshing()
self.labels.insert("3", at: 0)
self.labels.insert("2", at: 0)
self.labels.insert("1", at: 0)
self.tableView.reloadData()
let indexPath = IndexPath(row: 3, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
self.tableView.panGestureRecognizer.isEnabled = false
let when = DispatchTime.now() + 0.05
DispatchQueue.main.asyncAfter(deadline: when)
{
self.tableView.panGestureRecognizer.isEnabled = true
}
}
我发现这很老套,建议不要这样做。
关于ios - 如何在不滚动表格的情况下将行插入到 UITableView 的顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39901793/