ios 利用kvc 监听可变数组变化

KVO键值监听:

Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知。简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观察者了。

使用方式:

1. 注册,指定被观察者的属性,

2. 实现回调方法

3. 移除观察

上代码之前向大家说几个小坑,小白可以看,大神直接略过。

kvo直接监听NSMutableArray的时候,大家可能都认为count属于它的一个属性,当直接修改它的时候,count会发生变化,直接调用回调方法,事实上,kvo是无法直接监听count属性的,会直接崩溃,原因就不具体说了。怎么解决呢?我们可以新建一个model 来储存我们需要观察的NSMutableArray。好了,开始上代码;

新建 KVOModel.h

@interface KVOModel : NSObject

@property(nonatomic,strong)NSMutableArray *dataArray;

扫描二维码关注公众号,回复: 3379432 查看本文章

@end

ViewController中导入KVOModel.h

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)KVOModel *model;

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor=[UIColor whiteColor];

[self cteateUI];

[self loadDatasorce];

[self createBtn1];

[self createBtn2];

}

-(void)createBtn1{

UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];

[btn setTitle:@"change1" forState:UIControlStateNormal];

btn.frame=CGRectMake(0, 0, 50, 44);

[self.view addSubview:btn];

[btn addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)createBtn2{

UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];

[btn setTitle:@"change2" forState:UIControlStateNormal];

btn.frame=CGRectMake(414-50, 0, 50, 44);

[self.view addSubview:btn];

[btn addTarget:self action:@selector(click2:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)click1:(UIButton*)sender{

[[self.model mutableArrayValueForKey:@"dataArray"] addObject:[NSString stringWithFormat:@"%u",arc4random()%100-1]];

}

-(void)click2:(UIButton*)sender{

//数组进行删除的时候要使用kvc的方式

[[self.model mutableArrayValueForKey:@"dataArray"] removeLastObject];

}

-(void)cteateUI{

self.tableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

self.tableView.delegate=self;

self.tableView.dataSource=self;

[self.view addSubview:self.tableView];

}

-(void)loadDatasorce{

self.model=[[KVOModel alloc]init];

[self.model addObserver:self forKeyPath:@"dataArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];

self.model.dataArray=[NSMutableArray new];

for (NSInteger i=0;i<10; i++) {

//数组进行添加的时候要使用kvc的方式

[[self.model mutableArrayValueForKey:@"dataArray"] addObject:[NSString stringWithFormat:@"%ld",i]];

}

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{

if ([keyPath isEqualToString:@"dataArray"]) {

// KVOModel * kvoModel=(KVOModel *)object;

// if (kvoModel.dataArray.count>=10) {

[_tableView reloadData];

// }

}

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 30;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return  self.model.dataArray.count;

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cellid"];

if (!cell) {

cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellid"];

}

cell.textLabel.text= self.model.dataArray[indexPath.row];

return cell;

}

//移除观察者

-(void)dealloc{

if (_model != nil) {

[_model removeObserver:self forKeyPath:@"dataArray"];

}

}

上机测试,ok,增加,删除都会触发tableview的刷新。

猜你喜欢

转载自www.cnblogs.com/soulDn/p/9713957.html