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
    }
}

你可能感兴趣的:(UITableView grouped模式下section 去掉顶部间距)