如何在 ios Swift 中禁用 UITableView 部分标题滚动

问题描述 投票:0回答:1

滚动时,tableview 表部分标题不应从其位置滚动,只有表格单元格应可滚动。

我选择了UITableView样式普通并禁用了滚动时弹跳。但是,滚动时表格视图单元格仍然有点滚动。

ios swift iphone uitableview uitableviewsectionheader
1个回答
0
投票

您可以使用普通样式的 UITableView 并将表视图的sectionHeaderTopPadding 属性设置为 0。此外,您可以禁用弹跳效果以防止节标题的任何意外滚动。具体方法如下:

import UIKit

class YourTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Disable the bounce effect
        tableView.bounces = false

        // Set the section header top padding to 0
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        }
    }

    // Implement the UITableViewDataSource methods as needed
}

通过将 tableView.bounces 设置为 false,可以防止表视图弹跳,从而防止节标题的任何滚动。将 tableView.sectionHeaderTopPadding 设置为 0 可确保节标题保持固定在顶部,而无需任何额外的填充。

这应该会产生一个表格视图,其中只有表格单元格滚动,而节标题保持固定。请注意,sectionHeaderTopPadding 属性从 iOS 15 开始可用,因此请确保您的部署目标包含此 iOS 版本,或者如果您的目标是旧版本,请相应地使用它。

© www.soinside.com 2019 - 2024. All rights reserved.