Ios UITableView在插入行后滚动到顶部
我有一个可扩展的Ios UITableView在插入行后滚动到顶部,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个可扩展的UITableView。点击部分时,它们会随着动画(滚动)展开或折叠。我的问题是,在展开或折叠标题时会出现奇怪的动画UITableView滚动到顶部,然后转到点击的单元格。此外,当没有展开的单元格时,有时它不会滚动到顶部,并且在顶部标题和UITableView的顶部视图之间有很大的空间 我的问题是,我需要滚动到扩展的部分,并摆脱滚动到顶部的错误 我尝试了以下解决方案,但对我无效: 下面的问题看起来也是同样的问题,但不知道如何实现它。 如何切换选择: func toggleSe
UITableView
。点击部分时,它们会随着动画(滚动)展开或折叠。我的问题是,在展开或折叠标题时会出现奇怪的动画<代码>UITableView滚动到顶部,然后转到点击的单元格。此外,当没有展开的单元格时,有时它不会滚动到顶部,并且在顶部标题和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
}
如何声明标题强>
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)在插入行时看起来很有效。但是,我在删除行时仍然存在错误。底部标题转到tableview的中心,然后在折叠标题后转到底部
在我的代码中,我使用
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
}
}
}
另外,不要忘记相应地增加或减少该特定部分的行数。
前一行对于避免任何崩溃非常重要。您可以尝试使用以下代码。只需获取tableview的最后一个内容偏移量。然后执行更新并重新指定内容偏移量
let lastOffset = tableView.contentOffset
self.tableView.beginUpdates()
self.tableView.layer.removeAllAnimations()
self.tableView.endUpdates()
tableView.contentOffset = lastOffset
简单解决方案swift3及以上版本 使用下面的扩展名作为 例如:tableViewOutlet.tableViewScrollToBottom(动画:true)
我也面临着同样的问题,但在阅读了一些教程和研究与分析之后,我发现出现这个问题的原因是当您在tableview中展开部分时,单元格的高度从0到120(根据您的单元格高度)。 在我的例子中,我用估计的单元格高度解决了这个问题 我希望这能帮助你,
谢谢我正在使用可扩展的tableview..不需要为动画添加额外的代码..您可以查看一下它。很抱歉,我无法在我的项目中使用第三方库。我需要自己修复。你试过重新加载部分吗?它修复了滚动到顶部的错误,但知道有时底部单元格有重复的行。正如我看到的,这会导致底部单元格出现重复的行错误。如果我们能解决这个问题,我会把它标记为工作问题。您是否增加了扩展该部分的行数。还可以在UIView.animate中使用LayoutIfneeded