swift ClassNameFromString 的替换方法 + 创建TableviewHelper
阅读原文时间:2023年07月12日阅读:1

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 ;  
}

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  
        }else{  
            return  
        }  
    }

    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 ==  {

        let footerView = UIView.init(frame: CGRect(x:,y:,width:self.view.frame.size.width ,height:self.view.frame.size.height \* 0.5));  
        let footerLabel = UILabel.init(frame: CGRect(x:,y:,width:,height:));  
        footerLabel.text = "未查找到对应的报告";  
        footerLabel.font = UIFont.boldSystemFont(ofSize: );  
        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!)!) ==  ? "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;

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章