iOS中runtime的简单用法——动态绑定
2016-04-25 00:00
627 查看
摘要: runtime我就不介绍了,相信学好的童鞋一定是受益无穷了。为了写这个博客今天特意写了个例子,分享出来
先说需求:在tableView中每一行有一个删除按钮,点击删除当前行。(很多项目都会用到吧)
写一段废话:写例子的时候一直想找一个大家常用的功能做,但当这个例子写完我又犹豫要不要传上来,原因是对于这个功能,其实有其他更好的方法解决,其中我代码中的(方法一)就是其中一个不错的解决方案。可是如果你细心会发现,runtime有个很大的好处就是你不需要再费劲找目标对象了。只需要 1 绑定。2 取出。用法简单又霸道...
代码:
用法就这两行。
最后上传个效果图
先说需求:在tableView中每一行有一个删除按钮,点击删除当前行。(很多项目都会用到吧)
写一段废话:写例子的时候一直想找一个大家常用的功能做,但当这个例子写完我又犹豫要不要传上来,原因是对于这个功能,其实有其他更好的方法解决,其中我代码中的(方法一)就是其中一个不错的解决方案。可是如果你细心会发现,runtime有个很大的好处就是你不需要再费劲找目标对象了。只需要 1 绑定。2 取出。用法简单又霸道...
代码:
#import "ViewController.h" #import "SGMyNewsViewTableViewCell.h" #import <objc/runtime.h> @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> @property (nonatomic,strong) UITableView *tableView; @property (nonatomic,strong) NSMutableArray *dataList; @end @implementation ViewController static const void *deleteButtonKey = @"deleteButtonKey"; #pragma mark -- 懒加载 -(UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; [self.view addSubview:_tableView]; } return _tableView; } #pragma mark -- 生命周期 - (void)viewDidLoad { [super viewDidLoad]; self.title = @"Runtime"; //数据源 self.dataList = [NSMutableArray arrayWithObjects:@"这是一条新闻",@"6月13日苹果公司举行WWDC",@"科比于4月14日退役",@"Jeep国产后的粉丝经济怎么“玩”?", nil]; //注册cell [self.tableView registerClass:[SGMyNewsViewTableViewCell class] forCellReuseIdentifier:@"Cell"]; //去掉分割线 self.tableView.separatorColor = [UIColor colorWithWhite:1 alpha:0.]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark--UITableViewDataSource,UITableViewDelegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _dataList.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellId = @"Cell"; //从缓冲区中获取已有的cell SGMyNewsViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.contentLabel.text = self.dataList[indexPath.row]; cell.timeLabel.text = @"2016-4-20"; cell.titleLabel.text = [NSString stringWithFormat:@"新闻标题%@",@(indexPath.row+1)]; [cell.deleteButton addTarget:self action:@selector(deleteRow:) forControlEvents:UIControlEventTouchUpInside]; //方法二:runtime机制 首先 #import <objc/runtime.h> //绑定 objc_setAssociatedObject(cell.deleteButton, deleteButtonKey, indexPath, OBJC_ASSOCIATION_COPY_NONATOMIC); return cell; } //删除行 - (void)deleteRow:(UIButton *)button { //方法一 /* SGMyNewsViewTableViewCell *cell = (id)button.superview; while (![cell isKindOfClass:[SGMyNewsViewTableViewCell class]]) { cell = (id)cell.superview; } NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; [self.dataList removeObject:self.dataList[indexPath.row]]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:YES]; */ //方法二:runtime机制 //取出 NSIndexPath *indexPath = objc_getAssociatedObject(button, deleteButtonKey); [self.dataList removeObject:self.dataList[indexPath.row]]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:YES]; //方法二需要刷新 因为这里做删除处理,数组的count会变化,所以此处必须要刷新方法重新绑定赋值(如果在别的例子中只做传值,则不需要) [self.tableView reloadData]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 70; }
objc_setAssociatedObject(绑定对象, 全局Key, 传入对象, OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_getAssociatedObject(绑定对象, 全局Key);//返回值id类型--传入对象
用法就这两行。
OBJC_ASSOCIATION_COPY_NONATOMIC //这个参数有兴趣的可以点进去看看,其实还有其他几个选项,比如当你传字符串的时候这个参数要改为OBJC_ASSOCIATION_RETAIN_NONATOMIC。//我这里要传进来一个indexPath的对象,所以用了COPY。具体他们之间的区别以及用法,直接复制找百度大神吧。
最后上传个效果图
相关文章推荐
- Java Runtime Environment 5.0 Update 12 下载
- C++实现动态绑定代码分享
- 深入理解C++的动态绑定与静态绑定的应用详解
- php set_magic_quotes_runtime() 函数过时解决方法
- asp.net中一次性动态绑定多个droplistdown
- Asp.Net 程序错误Runtime Error原因与解决
- System 类 和 Runtime 类的常用用法介绍
- javascript下动态this与动态绑定实例代码
- PHP5多态性与动态绑定介绍
- Java中的静态绑定和动态绑定详细介绍
- 理解Java中的静态绑定和动态绑定
- AngularJS单选框及多选框实现双向动态绑定
- NET Runtime Optimization Service 1101 错误的解决方法
- 自动释放池的前世今生 ---- 深入解析 autoreleasepool
- 深入解析 ObjC 中方法的结构
- 你真的了解 load 方法么?
- 从源代码看 ObjC 中消息的发送
- IOS高级教程2:反射根据变量的引用获取变量名
- iOS学习之Objective-C 2.0 运行时系统编程
- iOS runtime原理