Emr*_*der 5 uitableview ios swift
我有一个可扩展的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
}
Run Code Online (Sandbox Code Playgroud)
我给出了内部单元格的高度;
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
Run Code Online (Sandbox Code Playgroud)
我如何声明标题;
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
}
Run Code Online (Sandbox Code Playgroud)
编辑:此问题中的解决方案(将估计高度设置为 0)在插入行时看起来像工作。但是,删除行时我仍然有错误。底部标题转到 tableview 的中心,然后在折叠标题后转到底部。
在我的代码中,我使用 和来扩展和收缩特定部分,而不是 和tableView.beginUpdates()
,当您需要提供部分扩展时,您可以调用 reloadData 。这样就不会出现动画滚动到顶部的问题。在我的项目中工作得很好,我必须在单击包含在该部分中的按钮时显示该部分中的行数。tableView.endUpdates()
tableView.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
}
}
}
Run Code Online (Sandbox Code Playgroud)
另外,不要忘记相应地增加或减少该特定部分的行数。
前一行对于避免任何崩溃很重要。
归档时间: |
|
查看次数: |
1636 次 |
最近记录: |