Swift中常用Extension(三)

772 阅读2分钟

UIScrollView

滚动到顶部扩展

public extension UIScrollView {
    func scrollToTop(_ animated: Bool = true) {
        var offset = contentOffset
        offset.y = 0 - contentInset.top
        setContentOffset(offset, animated: animated)
    }
}

滚动到底部扩展

public extension UIScrollView {
    func scrollToBottom(_ animated: Bool = true) {
        var offset = contentOffset
        offset.y = contentSize.height - bounds.size.height + contentInset.bottom
        setContentOffset(offset, animated: animated)
    }
}

滚动到左边扩展

public extension UIScrollView {
    func scrollToLeft(_ animated: Bool = true) {
        var offset = contentOffset
        offset.x = 0 - contentInset.left
        setContentOffset(offset, animated: animated)
    }
}

滚动到右边扩展

public extension UIScrollView {
    func scrollToRight(_ animated: Bool = true) {
        var offset = contentOffset
        offset.x = contentSize.width - bounds.size.width + contentInset.right
        setContentOffset(offset, animated: animated)
    }
}

UITableView

UITableViewCell的注册扩展

我们写UITableView的时候都会写一个cell的注册,但是每次都要写一个CellReuseIdentifier,真的很烦

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")

所以我们就给UITableView扩展一个方法,CellReuseIdentifier就使用cell的类名

public extension UITableView {
    /// Register UITableViewCell
    func register(cellWithClass name: AnyClass) {
        register(name, forCellReuseIdentifier: String(describing: name))
    }
}

这样使用就可以了

tableView.register(cellWithClass: UITableViewCell.self)

UITableViewCell的获取扩展

public extension UITableView {

    /// dequeue reusable UITableViewCell using class name
    func dequeueReusableCell<T: UITableViewCell>(withClass name: T.Type) -> T {
        guard let cell = dequeueReusableCell(withIdentifier: String(describing: name)) as? T else {
            fatalError("Couldn't find UITableViewCell for \(String(describing: name)), make sure the cell is registered with table view")
        }
        return cell
    }

    /// dequeue reusable UITableViewCell using class name for indexPath
    func dequeueReusableCell<T: UITableViewCell>(withClass name: T.Type, for indexPath: IndexPath) -> T {
        guard let cell = dequeueReusableCell(withIdentifier: String(describing: name), for: indexPath) as? T else {
            fatalError("Couldn't find UITableViewCell for \(String(describing: name)), make sure the cell is registered with table view")
        }
        return cell
    }
}

使用

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withClass: UITableViewCell.self)
    return cell
}

UICollectionView

UICollectionViewCell的注册扩展

public extension UICollectionView {
    func register(_ cellClass: AnyClass) {
        register(cellClass, forCellWithReuseIdentifier: "\(cellClass)")
    }
}

使用

collectionView.register(UICollectionViewCell.self)

UICollectionView Section Header的注册扩展

public extension UICollectionView {
    func registerSectionHeader(_ viewClass: AnyClass) {
        register(viewClass, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "\(viewClass)")
    }
}

使用

collectionView.registerSectionHeader(UICollectionReusableView.self)

UICollectionView Section Footer的注册扩展

public extension UICollectionView {
    func registerSectionFooter(_ viewClass: AnyClass) {
        register(viewClass, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "\(viewClass)")
    }
}

使用

collectionView.registerSectionFooter(UICollectionReusableView.self)

UICollectionViewCell的获取扩展

public extension UICollectionView {
    func dequeueReusableCell<T: UICollectionViewCell>(withClass name: T.Type, for indexPath: IndexPath) -> T {
        guard let cell = dequeueReusableCell(withReuseIdentifier: String(describing: name), for: indexPath) as? T else {
            fatalError("Couldn't find UICollectionViewCell for \(String(describing: name)), make sure the cell is registered with collection view")
        }
        return cell
    }
}

使用

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withClass: UICollectionViewCell.self, for: indexPath)
    return cell
}

UICollectionView Section Header的获取扩展

public extension UICollectionView {
    func dequeueSectionHeader<T: UICollectionReusableView>(withClass name: T.Type, for indexPath: IndexPath) -> T {
        guard let cell = dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: String(describing: name), for: indexPath) as? T else {
            fatalError("Couldn't find UICollectionReusableView for \(String(describing: name)), make sure the view is registered with collection view")
        }
        return cell
    }
}

使用

let view = collectionView.dequeueSectionHeader(withClass: UICollectionReusableView.self, for: indexPath)

UICollectionView Section Footer的获取扩展

public extension UICollectionView {
    func dequeueSectionFooter<T: UICollectionReusableView>(withClass name: T.Type, for indexPath: IndexPath) -> T {
        guard let cell = dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: String(describing: name), for: indexPath) as? T else {
            fatalError("Couldn't find UICollectionReusableView for \(String(describing: name)), make sure the view is registered with collection view")
        }
        return cell
    }
}

使用

let view = collectionView.dequeueSectionFooter(withClass: UICollectionReusableView.self, for: indexPath)