swift 的UITableView + Extension.swift

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 的UITableView + Extension.swift相关的知识,希望对你有一定的参考价值。

extension UITableView {
    
    /**
     Use it as a trick to remove the separator lines of blank cells at the bottom of tableView.
     */
    func removeBottomSeparatorLine() {
        tableFooterView = UIView(frame: CGRect.zero)
    }
    
    func scrollToLastRowOfSection(section: Int, atScrollPosition: UITableViewScrollPosition = .Top, animated: Bool = false) {
        scrollToRowAtIndexPath(lastRowInSection(section), atScrollPosition: atScrollPosition, animated: animated)
    }
    
    func registerClass(cell: UITableViewCell.Type) {
        registerClass(cell.classForCoder(), forCellReuseIdentifier: cell.className())
    }
    
    func registerNib(cell: UITableViewCell.Type) {
        registerNib(cell.nib(), forCellReuseIdentifier: cell.className())
    }
    
    func dequeueCell<T where T: UITableViewCell, T: ClassnameStringConvertable>(forIndexPath indexPath: NSIndexPath) -> T {
        guard let cell = dequeueReusableCellWithIdentifier(T.className(), forIndexPath: indexPath) as? T else {
            preconditionFailure("Unable to dequeue \(T.description()) for indexPath: \(indexPath)")
        }
        return cell
    }
    
    func registerHeaderFooterNib(headerFooterView: UITableViewHeaderFooterView.Type) {
        registerNib(headerFooterView.nib(), forHeaderFooterViewReuseIdentifier: headerFooterView.className())
    }
    
    func registerHeaderFooterClass(headerFooterView: UITableViewHeaderFooterView.Type) {
        registerClass(headerFooterView.classForCoder(), forHeaderFooterViewReuseIdentifier: headerFooterView.className())
    }
    
    func dequeueHeaderFooter<T where T: UITableViewHeaderFooterView, T: ClassnameStringConvertable>() -> T {
        guard let view = dequeueReusableHeaderFooterViewWithIdentifier(T.className()) as? T else {
            preconditionFailure("Unable to dequeue \(T.description())")
        }
        return view
    }
    
    private func lastRowInSection(section: Int) -> NSIndexPath {
        guard let dataSource = dataSource else {
            preconditionFailure("Don't have dataSource")
        }
        return NSIndexPath(forRow: dataSource.tableView(self, numberOfRowsInSection: section) - 1, inSection: section)
    }
}

以上是关于swift 的UITableView + Extension.swift的主要内容,如果未能解决你的问题,请参考以下文章

swift 的UITableView + Extension.swift

Swift - 无法向 UITableView 添加数据

UITableView 背景颜色 Swift

Swift 3:有弹性的 UITableView 页脚

SWIFT UITableView的基本用法

使用 Swift 的带有 MVVM 的 UITableView