Swift-TableViewCell 注册单元格不再担心重用标识符的问题了

388 阅读1分钟

背景:

我们在使用TableView 或 CollectionView 的时候,总是需要定义重用标识符,是不是觉得还挺麻烦的,而且自定义的单元格还要做类型转化。我真的受够了这样的写法了,所以我决定替你们分担这份忧愁。

先看看怎么使用吧

  • 注册cell
    • tableView.registerCell(EPRankingCell.self)
  • 获取重用(这里已经自动做了类型转化了,cell 就是EPRankingCell )
    • let cell = tableView.dequeueReusableCell(EPRankingCell.self)
TableViewCell的注册与使用
extension UITableView {

    /// 注册Cell
    public func registerCell<T: UITableViewCell>(_ type: T.Type) {
        let identifier = String(describing: type.self)
        self.register(type, forCellReuseIdentifier: identifier)
    }

    /// 注册NibCell
    public func registerNibCell<T: UITableViewCell>(_ type: T.Type) {
        let identifier = String(describing: type.self)
        self.register(UINib.init(nibName: identifier, bundle: nil), forCellReuseIdentifier: identifier)
    }

    /// 取重用Cell
    public func dequeueReusableCell<T: UITableViewCell>(_ type: T.Type) -> T {
        let identifier = String(describing: type.self)
        guard let cell = self.dequeueReusableCell(withIdentifier: identifier) as? T else {
            fatalError("\(type.self) was not registered")
        }
        return cell
    }

    /// 注册重用区头或去尾
    public func registerReuseView<T: UITableViewHeaderFooterView>(_ type: T.Type) {
        let identifier = String(describing: type.self)
        self.register(type, forHeaderFooterViewReuseIdentifier: identifier)
    }

    

    /// 重用区头或去尾
    public func dequeueReuseView<T: UITableViewHeaderFooterView>(_ type: T.Type) -> T {
        let identifier = String(describing: type.self)
        guard let reusableView = self.dequeueReusableHeaderFooterView(withIdentifier: identifier) as? T else {
            fatalError("\(type.self) was not registered")
        }
        return reusableView
    }

}
CollectionViewCell的注册与使用
extension UICollectionView {

    /// 注册Cell
    public func registerCell<T: UICollectionViewCell>(_ type: T.Type) {
        let identifier = String(describing: type.self)
        self.register(type, forCellWithReuseIdentifier: identifier)
    }

    /// 注册NibCell
    public func registerNibCell<T: UICollectionViewCell>(_ type: T.Type) {
        let identifier = String(describing: type.self)
        self.register(UINib.init(nibName: identifier, bundle: nil), forCellWithReuseIdentifier: identifier)
    }

    /// 取重用Cell
    public func dequeueReusableCell<T: UICollectionViewCell>(_ type: T.Type, for indexPath: IndexPath) -> T {
        let identifier = String(describing: type.self)
        guard let cell = self.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as? T else {
            fatalError("\(type.self) was not registered")
        }
        return cell
    }

    func registerFooterReuseView<T: UICollectionReusableView>(_ type: T.Type) {
        let identifier = String(describing: type.self)
        self.register(type.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: identifier)
    }

    func registerHeaderReuseView<T: UICollectionReusableView>(_ type: T.Type) {
        let identifier = String(describing: type.self)
        self.register(type.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: identifier)
    }

    func dequeueReuseView<T: UICollectionReusableView>(_ type: T.Type, kind: String, indexPath: IndexPath) -> T {
        let identifier = String(describing: type.self)
        guard let header = self.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: identifier, for: indexPath) as? T else {
            fatalError("\(type.self) was not registered")
        }
        return header
    }
}