我们继续。。
观察者的作用就是让控制器去观察Model的属性是否发生变化,如果属性有了新值,就重新赋值
上代码!!
第一步:注册观察者
// 如果movie.pictureimage为空的时候 执行下面的方法
if (movie.pictureimage == nil) {
// 注册观察者 (model类需要controller类成为自己的观察者 当自己有一个属性发生变化的时候 执行对应的方法)
[movie addObserver:self forKeyPath:@"pictureimage" options:(NSKeyValueObservingOptionNew) context:[indexPath retain]];
}
第二步:实现观察者的方法
// 实现观察者的方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSIndexPath *indexPath = (NSIndexPath *)context;
// 将传过来的indexpath转换为cell
MovieViewCell *cell = (MovieViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
// 获取当前显示的cell
NSArray *visibleCell = [self.tableView visibleCells];
// 判断获取的cell是否处于显示状态,如果处于显示状态才进行替换,否则什么都不做
if ([visibleCell containsObject:cell]) {
cell.imagelabel.image = change[@"new"];
}
第三步:移除观察者
// 移除观察者
[object removeObserver:self forKeyPath:keyPath context:context];
[indexPath release];
}
KVO观察者实现的方法 就是这三步 加入到Controller类的文件中 就可以实现先出来其他的数据 当图片下载完成后 图片显示出来
嘿嘿 异步加载图片就基本上完成了 还有什么不懂可以私信我~~~