protocol Reusable: class { static var reuseIndentifier: String {get} static var nib: UINib? {get} } extension Reusable { static var reuseIndentifier: String { return String(Self) } static var nib: UINib? { return nil } } extension UITableView { func registerReusableCell<T: UITableViewCell where T: Reusable>(_: T.Type) { if let nib = T.nib { self.registerNib(nib, forCellReuseIdentifier: T.reuseIndentifier) }else { self.registerClass(T.self , forCellReuseIdentifier: T.reuseIndentifier) } } func dequeueReusableCell<T: UITableViewCell where T: Reusable>(indexpath : NSIndexPath) -> T { return self.dequeueReusableCellWithIdentifier(T.reuseIndentifier, forIndexPath: indexpath) as! T } func registerReusableHeaderFooterView<T: UITableViewHeaderFooterView where T: Reusable>(_: T.Type) { if let nib = T.nib { self.registerNib(nib, forHeaderFooterViewReuseIdentifier: T.reuseIndentifier) } else { self.registerClass(T.self, forHeaderFooterViewReuseIdentifier: T.reuseIndentifier) } } func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView where T: Reusable>() -> T? { return self.dequeueReusableHeaderFooterViewWithIdentifier(T.reuseIndentifier) as! T? } } class JHTableViewCell: UITableViewCell, Reusable { static var nib: UINib? { return UINib(nibName: String(JHTableViewCell.self), bundle: nil) } override func awakeFromNib() { super.awakeFromNib() } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } } class CodeBasedCustomCell: UITableViewCell, Reusable { }
时间: 2024-12-30 01:12:31