您的位置:首页 > 移动开发 > IOS开发

iOS中runtime的简单用法——动态绑定

2016-04-25 00:00 627 查看
摘要: runtime我就不介绍了,相信学好的童鞋一定是受益无穷了。为了写这个博客今天特意写了个例子,分享出来

先说需求:在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。具体他们之间的区别以及用法,直接复制找百度大神吧。


最后上传个效果图



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  runtime 动态绑定