Swift:UITableView+Extension

JKSwiftExtension,測(cè)試用例在 UITableViewExtensionViewController.swift 里面
目錄:
1、基本的擴(kuò)展
2、鏈?zhǔn)骄幊?/p>

一、基本的擴(kuò)展
// MARK:- 一、基本的擴(kuò)展
public extension UITableView {

    // MARK: 1.1、tableView 在 iOS 11 上的適配
    /// tableView 在 iOS 11 上的適配
    func tableViewNeverAdjustContentInset() {
        if #available(iOS 11, *) {
            self.estimatedSectionFooterHeight = 0
            self.estimatedSectionHeaderHeight = 0
            self.contentInsetAdjustmentBehavior = .never
        }
    }

    // MARK: 1.2、是否滾動(dòng)到頂部
    /// 是否滾動(dòng)到頂部
    /// - Parameter animated: 是否要?jiǎng)赢?huà)
    func scrollToTop(animated: Bool) {
        setContentOffset(CGPoint(x: 0, y: 0), animated: animated)
    }

    // MARK: 1.3、是否滾動(dòng)到底部
    /// 是否滾動(dòng)到底部
    /// - Parameter animated: 是否要?jiǎng)赢?huà)
    func scrollToBottom(animated: Bool) {
        let y = contentSize.height - frame.size.height
        if y < 0 { return }
        setContentOffset(CGPoint(x: 0, y: y), animated: animated)
    }

    // MARK: 1.4、滾動(dòng)到什么位置(CGPoint)
    /// 是否滾動(dòng)到底部
    /// - Parameter animated: 是否要?jiǎng)赢?huà)
    func scrollToOffset(offsetX: CGFloat = 0, offsetY: CGFloat = 0, animated: Bool) {
        setContentOffset(CGPoint(x: offsetX, y: offsetY), animated: animated)
    }
}
二、鏈?zhǔn)骄幊?/h5>
// MARK:- 二、鏈?zhǔn)骄幊?public extension UITableView {

    // MARK: 2.1、設(shè)置 delegate 代理
    /// 設(shè)置 delegate 代理
    /// - Parameter delegate: delegate description
    /// - Returns: 返回自身
   @discardableResult
    func delegate(_ delegate: UITableViewDelegate) -> Self {
        self.delegate = delegate
        return self
    }

    // MARK: 2.2、設(shè)置 dataSource 代理
    /// 設(shè)置 dataSource 代理
    /// - Parameter dataSource: dataSource description
    /// - Returns: 返回自身
    @discardableResult
    func dataSource(_ dataSource: UITableViewDataSource) -> Self {
        self.dataSource = dataSource
        return self
    }

    // MARK: 2.3、設(shè)置行高
    /// 設(shè)置行高
    /// - Parameter height: 行高
    /// - Returns: 返回自身
    @discardableResult
    func rowHeight(_ height: CGFloat) -> Self {
        self.rowHeight = height
        return self
    }

    // MARK: 2.4、設(shè)置段頭(sectionHeaderHeight)的高度
    /// 設(shè)置段頭(sectionHeaderHeight)的高度
    /// - Parameter height: 段頭的高度
    /// - Returns: 返回自身
    @discardableResult
    func sectionHeaderHeight(_ height: CGFloat) -> Self {
        self.sectionHeaderHeight = height
        return self
    }

    // MARK: 2.5、設(shè)置段尾(sectionHeaderHeight)的高度
    /// 設(shè)置段尾(sectionHeaderHeight)的高度
    /// - Parameter height: 段尾的高度
    /// - Returns: 返回自身
    @discardableResult
    func sectionFooterHeight(_ height: CGFloat) -> Self {
        self.sectionFooterHeight = height
        return self
    }

    // MARK: 2.6、設(shè)置一個(gè)默認(rèn)cell高度
    /// 設(shè)置一個(gè)默認(rèn)cell高度
    /// - Parameter height: 默認(rèn)cell高度
    /// - Returns: 返回自身
    @discardableResult
    func estimatedRowHeight(_ height: CGFloat) -> Self {
        self.estimatedRowHeight = height
        return self
    }

    // MARK: 2.7、設(shè)置默認(rèn)段頭(estimatedSectionHeaderHeight)高度
    /// 設(shè)置默認(rèn)段頭(estimatedSectionHeaderHeight)高度
    /// - Parameter height: 段頭高度
    /// - Returns: 返回自身
    @discardableResult
    func estimatedSectionHeaderHeight(_ height: CGFloat) -> Self {
        self.estimatedSectionHeaderHeight = height
        return self
    }

    // MARK: 2.8、設(shè)置默認(rèn)段尾(estimatedSectionFooterHeight)高度
    /// 設(shè)置默認(rèn)段尾(estimatedSectionFooterHeight)高度
    /// - Parameter height: 段尾高度
    /// - Returns: 返回自身
    @discardableResult
    func estimatedSectionFooterHeight(_ height: CGFloat) -> Self {
        self.estimatedSectionFooterHeight = height
        return self
    }

    // MARK: 2.9、設(shè)置分割線(xiàn)的樣式
    /// 設(shè)置分割線(xiàn)的樣式
    /// - Parameter style: 分割線(xiàn)的樣式
    /// - Returns: 返回自身
    @discardableResult
    func separatorStyle(_ style: UITableViewCell.SeparatorStyle = .none) -> Self {
        self.separatorStyle = style
        return self
    }

    // MARK: 2.10、設(shè)置 UITableView 的頭部 tableHeaderView
    /// 設(shè)置 UITableView 的頭部 tableHeaderView
    /// - Parameter head: 頭部 View
    /// - Returns: 返回自身
    @discardableResult
    func tableHeaderView(_ head: UIView?) -> Self {
        self.tableHeaderView = head
        return self
    }

    // MARK: 2.11、設(shè)置 UITableView 的尾部 tableFooterView
    /// 設(shè)置 UITableView 的尾部 tableFooterView
    /// - Parameter foot: 尾部 View
    /// - Returns: 返回自身
    @discardableResult
    func tableFooterView(_ foot: UIView?) -> Self {
        self.tableFooterView = foot
        return self
    }

    // MARK: 2.12、滾動(dòng)到第幾個(gè)IndexPath
    /// 滾動(dòng)到第幾個(gè)IndexPath
    /// - Parameters:
    ///   - indexPath: 第幾個(gè)IndexPath
    ///   - scrollPosition: 滾動(dòng)的方式
    ///   - animated: 是否要?jiǎng)赢?huà)
    /// - Returns: 返回自身
    @discardableResult
    func scroll(to indexPath: IndexPath, at scrollPosition: UITableView.ScrollPosition = .middle, animated: Bool = true) -> Self {
        if indexPath.section < 0 || indexPath.row < 0 || indexPath.section > self.numberOfSections || indexPath.row > self.numberOfRows (inSection: indexPath.section) {
            return self
        }
        scrollToRow(at: indexPath, at: scrollPosition, animated: animated)
        return self
    }

    // MARK: 2.13、滾動(dòng)到第幾個(gè)row、第幾個(gè)section
    /// 滾動(dòng)到第幾個(gè)row、第幾個(gè)section
    /// - Parameters:
    ///   - row: 第幾 row
    ///   - section: 第幾 section
    ///   - scrollPosition: 滾動(dòng)的方式
    ///   - animated: 是否要?jiǎng)赢?huà)
    /// - Returns: 返回自身
    @discardableResult
    func scroll(row: Int, section: Int = 0, at scrollPosition: UITableView.ScrollPosition = .middle, animated: Bool = true) -> Self {
        return scroll(to: IndexPath.init(row: row, section: section), at: scrollPosition, animated: animated)
    }

    // MARK: 2.14、滾動(dòng)到最近選中的cell(選中的cell消失在屏幕中,觸發(fā)事件可以滾動(dòng)到選中的cell)
    /// 滾動(dòng)到最近選中的cell(選中的cell消失在屏幕中,觸發(fā)事件可以滾動(dòng)到選中的cell)
    /// - Parameters:
    ///   - scrollPosition: 滾動(dòng)的方式
    ///   - animated: 是否要?jiǎng)赢?huà)
    /// - Returns: 返回自身
    @discardableResult
    func scrollToNearestSelectedRow(scrollPosition: UITableView.ScrollPosition = .middle, animated: Bool = true) -> Self {
        scrollToNearestSelectedRow(at: scrollPosition, animated: animated)
        return self
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容