美文网首页
重构:Swift 下 UITableViewCell 极简调用

重构:Swift 下 UITableViewCell 极简调用

作者: SoaringHeart | 来源:发表于2019-09-27 10:29 被阅读0次
    🌰🌰: 
     let cell = tableView.dequeueReusableCell(for: UITableViewCellDateRange.self)
     let cell = tableView.dequeueReusableCell(for: UITableViewCellDateRange())
    
    public extension UITableView{
        
        /// 泛型复用cell - cellType: "类名.self" (备用默认值 T.self)
        final func dequeueReusableCell<T: UITableViewCell>(for cellType: T.Type, identifier: String = String(describing: T.self), style: UITableViewCell.CellStyle = .default) -> T{
            //        let identifier = String(describing: T.self)
            var cell = self.dequeueReusableCell(withIdentifier: identifier);
            if cell == nil {
                cell = T.init(style: style, reuseIdentifier: identifier);
            }
            
            cell!.selectionStyle = .none;
            cell!.separatorInset = .zero;
            cell!.layoutMargins = .zero;
            return cell! as! T;
        }
        
        /// 泛型复用cell - aClass: "类名()"
        final func dequeueReusableCell<T: UITableViewCell>(for aClass: T, identifier: String = String(describing: T.self), style: UITableViewCell.CellStyle = .default) -> T{
            return dequeueReusableCell(for: T.self, identifier: identifier, style: style)
        }
        
        /// 泛型复用HeaderFooterView - cellType: "类名.self" (备用默认值 T.self)
        final func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>(for cellType: T.Type, identifier: String = String(describing: T.self)) -> T{
            var cell = self.dequeueReusableHeaderFooterView(withIdentifier: identifier);
            if cell == nil {
                cell = T.init(reuseIdentifier: identifier);
            }
            cell!.layoutMargins = .zero;
            return cell! as! T;
        }
        
        /// 泛型复用HeaderFooterView - aClass: "类名()"
        final func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>(for aClass: T, identifier: String = String(describing: T.self)) -> T{
            return dequeueReusableHeaderFooterView(for: T.self, identifier: identifier)
        }
    }
    
    (OC 兼容方法)
    🌰: let cell = UITableViewCellFee.dequeueReusableCell(tableView);
    
    @objc public extension UITableViewCell{
        /// [源]自定义 UITableViewCell 获取方法
        static func dequeueReusableCell(_ tableView: UITableView, identifier: String = String(describing: self), style: UITableViewCell.CellStyle = .default) -> Self {
            var cell = tableView.dequeueReusableCell(withIdentifier: identifier);
            if cell == nil {
                cell = self.init(style: style, reuseIdentifier: identifier);
            }
    
            cell!.selectionStyle = .none;
            cell!.separatorInset = .zero;
            cell!.layoutMargins = .zero;
            return cell as! Self;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:重构:Swift 下 UITableViewCell 极简调用

          本文链接:https://www.haomeiwen.com/subject/vynzectx.html