UITableView grouped模式下section 去掉顶部间距

未去掉前:

simulator_screenshot_28131C10-CFFD-4B73-925D-1A0D69C57DA6.png

去掉后

simulator_screenshot_BE77C65E-9667-4F46-B2C3-955176E7BBF7.png

核心代码:

UITableView

        tableView = UITableView(frame: frame, style: .grouped)
        tableView.backgroundColor = UIColor.init(red: 1, green: 1, blue: 1, alpha: 0.3)
    
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false

UITableViewDataSource、UITableViewDelegate

extension BaseIndexView: UITableViewDataSource, UITableViewDelegate {
 
   ...

    public func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    
    public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return getHeightForFooterInSection()
    }
    
    public func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    
    public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return getHeightForHeaderInSection()
    }
    
    //grouped设置分组头部的高度
    //UITableView.automaticDimension:自动测量
    //要取消分组头部的高度,请设置0.01或 CGFloat.leastNormalMagnitude
    func getHeightForHeaderInSection() -> CGFloat{
        return CGFloat.leastNormalMagnitude
    }

    func getHeightForFooterInSection() -> CGFloat{
        return CGFloat.leastNormalMagnitude
    }
}

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容