ios – UITableView在插入行后滚动到顶部

ios – UITableView在插入行后滚动到顶部,第1张

概述我有一个可扩展的UITableView.当节拍片段时,它们会以动画展开或折叠(滚动).我的问题是在扩展或折叠标题时有一个奇怪的动画. UITableView滚动到顶部,然后转到tapped单元格.此外,当没有扩展的单元格时,有时候,它不会滚动到顶部,并且UITableView的顶部标题和顶视图之间有一个很大的空间. 我的问题是我需要滚动到扩展部分,并摆脱滚动到顶部的错误. 我尝试了下面的解决方案, 我有一个可扩展的UItableVIEw.当节拍片段时,它们会以动画展开或折叠(滚动).我的问题是在扩展或折叠标题时有一个奇怪的动画. UItableVIEw滚动到顶部,然后转到tapped单元格.此外,当没有扩展的单元格时,有时候,它不会滚动到顶部,并且UItableVIEw的顶部标题和顶视图之间有一个很大的空间.

我的问题是我需要滚动到扩展部分,并摆脱滚动到顶部的错误.

我尝试了下面的解决方案,但没有为我工作:
prevent table view to scrolling top after insertRows

对于下面的问题,它看起来也是同样的问题,但无法弄清楚如何实现它.
Why does my UITableView “jump” when inserting or removing a row?

我如何切换选择:

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!,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)看起来像插入行时工作.但是,删除行时仍然有BUG.底部标题转到tablevIEw的中心,然后在折叠标题后转到底部.

iOS 11 Floating TableView Header

解决方法 您可以尝试使用以下代码.只需获取tablevIEw的最后一个内容偏移量.然后执行更新并重新分配内容偏移量.

let lastOffset = tableVIEw.contentOffsetself.tableVIEw.beginUpdates()self.tableVIEw.layer.removeAllAnimations()self.tableVIEw.endUpdates()tableVIEw.contentOffset = lastOffset
总结

以上是内存溢出为你收集整理的ios – UITableView在插入行后滚动到顶部全部内容,希望文章能够帮你解决ios – UITableView在插入行后滚动到顶部所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1033417.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存