前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS中runtime的简单用法——动态绑定

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

作者头像
Raindew
发布2018-06-14 14:33:36
1K0
发布2018-06-14 14:33:36
举报

先说需求:在tableView中每一行有一个删除按钮,点击删除当前行。(很多项目都会用到吧) 写一段废话:写例子的时候一直想找一个大家常用的功能做,但当这个例子写完我又犹豫要不要传上来,原因是对于这个功能,其实有其他更好的方法解决,其中我代码中的(方法一)就是其中一个不错的解决方案。可是如果你细心会发现,runtime有个很大的好处就是你不需要再费劲找目标对象了。只需要 1 绑定。2 取出。用法简单又霸道... 代码:

代码语言:javascript
复制
  #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;
}
代码语言:javascript
复制
objc_setAssociatedObject(绑定对象, 全局Key, 传入对象, OBJC_ASSOCIATION_COPY_NONATOMIC);
代码语言:javascript
复制
objc_getAssociatedObject(绑定对象, 全局Key);//返回值id类型--传入对象

用法就这两行。

代码语言:javascript
复制
OBJC_ASSOCIATION_COPY_NONATOMIC //这个参数有兴趣的可以点进去看看,其实还有其他几个选项,比如当你传字符串的时候这个参数要改为OBJC_ASSOCIATION_RETAIN_NONATOMIC。//我这里要传进来一个indexPath的对象,所以用了COPY。具体他们之间的区别以及用法,直接复制找百度大神吧。

iOS技术交流群:511860085 成堆的技术视频福利,欢迎加入! 最后上传个效果图

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.02.16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档