swift TableView Cell Dequeue Trick

Posted

tags:

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

public protocol CellProtocol: class {
  static var identifier: String { get }
  static var nib: UINib { get }    
}

extension UITableViewCell: CellProtocol {
  // make sure the identifier is set to Class name
  public static var identifier: String {
    return String(describing: self)
  }
  public static var nib: UINib {
    return UINib(
      nibName: identifier, 
      bundle: Bundle(for: self)
    )
  }  
}

public extension UITableView {
  func register<T: CellProtocol>(cell: T.Type) {
    self.register(
      cell.nib, 
      forCellReuseIdentifier: cell.identifier
    )
  }
  func deque<T: CellProtocol>(cell: T.Type) -> T {
    return self.dequeueReusableCell(
      withIdentifier: cell.identifier
    )! as! T
  }
  func deque<T: CellProtocol>(cell: T.Type, for indexPath: IndexPath) -> T {
    return self.dequeueReusableCell(
        withIdentifier: cell.identifier, 
        for: indexPath
    ) as! T  
  }
}

以上是关于swift TableView Cell Dequeue Trick的主要内容,如果未能解决你的问题,请参考以下文章