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

swift ClassNameFromString 的替换方法 + 创建TableviewHelper的更多相关文章

  1. java 集合Collections 工具类:排序,查找替换。Set、List、Map 的of方法创建不可变集合

    Collections 工具类 Java 提供1个操作 Set List Map 等集合的工具类 Collections ,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合 ...

  2. 【Swift学习】Swift编程之旅---方法(十五)

    在Swift中结构体和枚举也能够定义方法,而在 Objective-C 中,类是唯一能定义方法的类型. 实例方法 实例方法是属于某个特定类.结构体或者枚举类型实例的方法,实例方法提供访问和修改实例属性 ...

  3. iOS开发之swift与OC混编出现的坑,oc中不能对swift的代理进行调用,不能访问swift中的代理,swift中的回调方法

    1. Swift与oc混编译具体怎么实现,这儿我就不重复讲出了,网上有大把的人讲解. 2. 在swift与OC混编的编译环境下, oc类不能访问swift创建类中的代理? 解决方法如下: 在代理的头部 ...

  4. android studio学习----Android Studio导入github下载的工程--替换方法

    http://www.cnblogs.com/liuling/p/2015-9-16-01.html 这种方法是可行的,主要是先自己创建一个project ,然后把没有的文件夹都复制过去就OK了,特别 ...

  5. 测试Javacript里的checkbox是否被选中的status和checked的替换方法

    测试Javacript里的checkbox是否被选中的status和checked的替换方法,checkbox.checked和checkbox.status的功能一样,注意checkbox.stat ...

  6. [Java] - 格式字符串替换方法

    Java 字符串格式替换方法有两种,一种是使用String.format(...),另一种是使用MessageFormat.format(...) 如下: import java.text.Messa ...

  7. Swift编程语言中的方法引用

    由于Apple官方的<The Swift Programming Guide>对Swift编程语言中的方法引用介绍得不多,所以这里将更深入.详细地介绍Swift中的方法引用. Swift与 ...

  8. PHP mkdir 方法 创建 0777 权限的目录问题

    php 中使用 mkdir() 方法创建 0777 权限的目录: $path = './Logs/secondCheck/';if(!is_dir($path)){ mkdir($path, 0777 ...

  9. console.log的一个应用 -----用new方法生成一个img对象和document.createElement方法创建一个img对象的区别

    我用两种方法来生成img对象,第一种方法是用new方法,第二种方法是用document.createElement方法. var img1 = new Image(); var img2 = docu ...

随机推荐

  1. 39、扩展原理-BeanFactoryPostProcessor

    39.扩展原理-BeanFactoryPostProcessor BeanPostProcessor:bean后置处理器,bean创建对象初始化前后进行拦截工作的 BeanFactoryPostPro ...

  2. WebService操作

    webservice是一种服务器通信技术,封装了socket.

  3. How To Add User To Sudoers On Debian 10 Buster

    In today’s tutorial, we are going to see how you can add a user to sudoers on Debian distributions. ...

  4. error C4995: “wcscpy”: 名称被标记为 #pragma deprecated

    1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.16.27023\atlm ...

  5. 2019-2020 ICPC, NERC, Southern and Volga Russian Regional Contest

    目录 Contest Info Solutions A. Berstagram B. The Feast and the Bus C. Trip to Saint Petersburg E. The ...

  6. 【概率论】5-6:正态分布(The Normal Distributions Part II)

    title: [概率论]5-6:正态分布(The Normal Distributions Part II) categories: - Mathematic - Probability keywor ...

  7. mysql触发器个人实战

    create trigger idtriggerbefore insert on flow_management_copy1for each ROWBEGIN SET new.ID= CONCAT(R ...

  8. PullToRefresh原理解析,pulltorefresh解析

    PullToRefresh原理解析,pulltorefresh解析 代码届有一句非常经典的话:"不要重复制造轮子",多少人看过之后便以此为本,把鲁迅的"拿来主义" ...

  9. 【解决方案】IP代理池设计与解决方案

    一.背景 爬虫服务请求量大,为了应对反爬措施,增加爬虫的爬取效率和代理IP使用率,需要设计一个IP代理池,满足以下需求: 定时任务获取第三方代理 及时剔除IP代理池中失效的IP 业务隔离IP 若IP未 ...

  10. GIAC 技术大会 Redis 演讲文字稿

    附录:https://mp.weixin.qq.com/s/mvAkPXBayAzT_RWFdsOt5A 观众朋友们,我是来自掌阅的工程师钱文品,今天我带来的是分享主题是:Redis 在海量数据和高并 ...