[英]UITableView Scrolls to top after insert row
我有一个可扩展的UITableView
。 点按各个部分时,它们会随着动画(滚动)展开或折叠。 我的问题是,展开或折叠标题时出现奇怪的动画。 UITableView
滚动到顶部,然后转到被点击的单元格。 另外,有时没有展开的单元格时,它不会滚动到顶部,并且UITableView
顶部标题和顶部视图之间有很大的空间。
我的问题是我需要滚动到展开的部分,也要摆脱滚动到顶部的错误。
我尝试了以下解决方案,但对我没有用: 防止表格视图在insertRows之后滚动到顶部
它也与下面的问题看起来一样,但是无法弄清楚如何实现它。 为什么插入或删除行时UITableView会“跳”?
如何切换选择:
func toggleSection(header: DistrictTableViewHeader, section: Int) {
print("Trying to expand and close section...")
// Close the section first by deleting the rows
var indexPaths = [IndexPath]()
for row in self.cities[section].districts.indices {
print(0, row)
let indexPath = IndexPath(row: row, section: section)
indexPaths.append(indexPath)
}
let isExpanded = self.cities[section].isExpanded
if(isExpanded){
AnalyticsManager.instance.logPageEvent(screenName: analyticsName!, category: "Button", action: Actions.interaction, label: "\(self.cities[section].name) Collapse Click")
}else{
AnalyticsManager.instance.logPageEvent(screenName: analyticsName!, category: "Button", action: Actions.interaction, label: "\(self.cities[section].name) Expand Click")
}
self.cities[section].isExpanded = !isExpanded
// This call opens CATransaction context
CATransaction.begin()
// This call begins tableView updates (not really needed if you only make one insertion call, or one deletion call, but in this example we do both)
tableView.beginUpdates()
if isExpanded {
tableView.deleteRows(at: indexPaths, with: .automatic)
} else {
tableView.insertRows(at: indexPaths, with: .automatic)
}
// completionBlock will be called after rows insertion/deletion animation is done
CATransaction.setCompletionBlock({
// This call will scroll tableView to the top of the 'section' ('section' should have value of the folded/unfolded section's index)
if !isExpanded{
self.tableView.scrollToRow(at: IndexPath(row: NSNotFound, section: section) /* you can pass NSNotFound to scroll to the top of the section even if that section has 0 rows */, at: .top, animated: true)
}
})
if self.scrollToTop(){
self.tableView.setContentOffset(.zero, animated: true)
}
// End table view updates
tableView.endUpdates()
// Close CATransaction context
CATransaction.commit()
}
private func scrollToTop() -> Bool{
for sec in self.cities{
if(sec.isExpanded){
return false
}
}
return true
}
我给里面的牢房高度;
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
我如何声明标头;
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = DistrictTableViewHeader()
header.isColapsed = !self.cities[section].isExpanded
header.customInit(title: self.cities[section].name, section: section, delegate: self)
return header
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
编辑:此问题(将估计高度设置为0)中的解决方案看起来像在插入行时工作。 但是,删除行时我仍然有错误。 底部标题转到表格视图的中心,然后在折叠标题之后转到底部。
您可以尝试使用以下代码。 只需获取表视图的最后一个内容偏移即可。 然后进行更新并重新分配内容偏移量。
let lastOffset = tableView.contentOffset
self.tableView.beginUpdates()
self.tableView.layer.removeAllAnimations()
self.tableView.endUpdates()
tableView.contentOffset = lastOffset
在我的代码中,我使用tableView.reloadData()
来扩展和收缩特定部分,而不是tableView.beginUpdates()
和tableView.endUpdates()
,可以在需要提供部分扩展时调用reloadData。
这样就不会出现动画滚动到顶部的问题。 并在我的项目中运行良好,在该项目中,必须单击该部分中包含的按钮,才能显示该部分中的特定行数。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
}
// Ignore the If Else statements it's just when do you need expansion of section.
else {
if showMore == true {
return self.userPoints.rewardsData[section - 1].count - 1
}
else {
return 0
}
}
}
同样不要忘记相应增加或减少该特定部分的行数。
前一行很重要,可以避免崩溃。
swift3及以上的简单解决方案
使用以下扩展名作为
例如:tableViewOutlet.tableViewScrollToBottom(动画:true)
extension UITableView {
func tableViewScrollToBottom(animated: Bool) {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
let numberOfSections = self.numberOfSections
let numberOfRows = self.numberOfRows(inSection: numberOfSections-1)
if numberOfRows > 0 {
let indexPath = IndexPath(row: 0, section: 0)
self.scrollToRow(at: indexPath, at: UITableView.ScrollPosition.top, animated: animated)
}
}
}
}
我也面临着同样的问题,但是在阅读了一些教程以及研究与分析后,我发现此问题的发生是由于在单元格的表格视图计数高度从0扩展到120(根据您的单元格高度)时展开单元格的高度。 在我的情况下,我使用估计的单元格高度解决了该问题。
希望对您有所帮助,谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.