HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 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的最后一个内容偏移量.然@L_450_23@更新并重新分配内容偏移量.

let lastOffset = tableView.contentOffset
self.tableView.beginupdates()
self.tableView.layer.removeAllAnimations()
self.tableView.endupdates()
tableView.contentOffset = lastOffset

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。