swift ClassNameFromString 的替换方法 + 创建TableviewHelper

1. ClassNameFromString 的方法oc 可以正常使用,但是swift 不能直接使用的,下面的代码

func getAPPName() -> String?{
    
    let nameKey = "CFBundleName" //或用  "CFBundleExecutable"
    guard let appName = Bundle.main.object(forInfoDictionaryKey: nameKey) as? String else {
        return nil;
    }
    return appName
}

func getClassNameFromeString(_ classString :String)->AnyClass?{
    
    guard let nameSpage = getAPPName() else {
        print("没有命名空间")
        return nil;
    }
    guard let childClass = NSClassFromString(nameSpage + "." + classString) else {
        print("没有获取到对应的class")
        return nil;
    }
    guard let childType = childClass as? UITableViewCell.Type else {
        print("没有得到的类型")
        return nil;
    }
    return childType.self;
}

2.利用这个方法,实现  tableview 简单的助理类

class TableViewHelper : NSObject ,UITableViewDataSource,UITableViewDelegate {

    var rowHeight:CGFloat?
    
    var cellForRow :((_ :UITableView,IndexPath)->UITableViewCell)?
    var heightForRow :((_ :IndexPath)->CGFloat)?
    var cellWillDisplay :((_ :UITableViewCell,IndexPath)->Void)?
    
    var selectRow :((_ :IndexPath)->Void)?;
    

    func addTableHeplerWith(tableView:UITableView,cellIdentArray:[(cellClass:String,cellIdent:String,isNib:Bool)]) ->Void{
    
        self.t_tableView = tableView;
        self.t_cellIdentArray = cellIdentArray;
        self.initSettings();
    }
    
    func reloaTableView(modelArray:[Any]) -> Void {
        
        t_models = modelArray;
        t_tableView?.reloadData();
    }
    
    //****************  私有 方法变量 分割线  ****************//
  
    private lazy var t_models :[Any] = {
        
        return [Any]();
    }()
    
    private lazy var t_cellIdentArray : [(cellClass:String,cellIdent:String,isNib:Bool)] = {
        
        return [(cellClass:String,cellIdent:String,isNib:Bool)]();
    }();
    
    private var t_tableView : UITableView?
    
    private func initSettings()->Void{
        
        t_tableView?.delegate = self;
        t_tableView?.dataSource = self;
        
        for tuple in t_cellIdentArray{
            
            if tuple.isNib == false {
                
                let cellClassName = getClassNameFromeString(tuple.cellClass) as! UITableViewCell.Type;
                t_tableView?.register(cellClassName, forCellReuseIdentifier: tuple.cellIdent);
                
            }else{
                
                t_tableView?.register(UINib.init(nibName: tuple.cellClass, bundle: nil), forCellReuseIdentifier: tuple.cellIdent);
            }
        }
    }

    // tableView dataSource method
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    
        return t_models.count
    }
   
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    
        if cellForRow != nil {
            
            return cellForRow!(tableView,indexPath);
        }else{
            
            print("tableView 调用不到对应的 cell");
            return UITableViewCell();
        }
    }

    //tableView delegate method
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        
        if heightForRow != nil {
            
            return heightForRow!(indexPath);
        }
        if  rowHeight != nil {
            
            return rowHeight!
        }
        return 50;
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath){
    
        if cellWillDisplay != nil {
            
            return cellWillDisplay!(cell,indexPath);
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
    
        tableView.deselectRow(at: indexPath, animated: true);
        
        if selectRow != nil {
            selectRow!(indexPath);
        }
    }

    
}

使用这个助理

let tableView = UITableView();
  let helper = TableViewHelper();

 func prepareHelperAction() -> Void {
        
        var cellIdents:[(String,String,Bool)];
        if is_iPad() {
            
            cellIdents = [("CompletedPadCell",completedCellID,false)];
        }else{
            
            cellIdents = [("CompletedCell",completedCellID,false)];
        }
        
        helper.addTableHeplerWith(tableView: tableView, cellIdentArray: cellIdents);
        
        helper.heightForRow = { (indexPath) in
            
            if is_iPad() {
                return 125
            }else{
                return 160
            }
        }
        
        helper.cellForRow = {  (table,indexPath) in
            
            if is_iPad() {
                
                let  cell = table.dequeueReusableCell(withIdentifier: self.completedCellID) as! CompletedPadCell;
                cell.configCellWithModel(self.models[indexPath.row] );
                cell.delegate = self;
                cell.indexPath = indexPath;
                return cell;
                
            }else{
                
                let  cell = table.dequeueReusableCell(withIdentifier: self.completedCellID) as! CompletedCell;
                cell.configCellWithModel(self.models[indexPath.row] );
                cell.delegate = self;
                cell.indexPath = indexPath;
                return cell;
            }
        }
    }
    
    func  reloadUI(_ dataModels:[CPModel]) -> Void {
        
        if dataModels.count == 0 {
            
            let footerView = UIView.init(frame: CGRect(x:0,y:0,self.view.frame.size.width ,height:self.view.frame.size.height * 0.5));
            let footerLabel = UILabel.init(frame: CGRect(x:0,y:0,200,height:40));
            footerLabel.text = "未查找到对应的报告";
            footerLabel.font = UIFont.boldSystemFont(ofSize: 17);
            footerLabel.textAlignment = .center;
            footerLabel.textColor = UIColor.gray;
            footerView.addSubview(footerLabel);
            footerLabel.center = footerView.center ;
            
            tableView.tableFooterView = footerView
      
        }else{
            
            self.models = dataModels;
            tableView.tableFooterView = UIView.init(frame: CGRect.zero);
        }
       
        helper.reloaTableView(modelArray: models);
    }

 cell he cell 的基类

class BaseCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }

    
    func configCellWithModel(_ model:Any)->Void{
        return;
    }
}
class CompletedPadCell: BaseCell {

    
    var delegate :CompletedCellDelegate?
    var indexPath :IndexPath?
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    required init?(coder aDecoder:NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style:UITableViewCellStyle, reuseIdentifier:String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI();
    }
    private func setUpUI() {
        
     }override func configCellWithModel(_ model:Any)->Void{
        
        let cellModel = model as? CPModel;
        
        self.acsLabel.text = Int((cellModel?.pri!)!) == 0 ? "ACS":"";
        self.nameLabel.text = cellModel?.pat_name
        self.sexLabel.text = cellModel?.pat_gender
        self.ageLabel.text = cellModel?.pat_age
        self.codeLabe.text = cellModel?.sid
        self.timeLabel.text = cellModel?.date;
  
}