观察者KVO
2015-09-01 19:36
288 查看
实观察者的回调方法现
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { // 根据观察的内容拿到图片 UIImage *image = [change objectForKey:NSKeyValueChangeNewKey]; NSIndexPath *indexPath = (__bridge NSIndexPath *)context; // 拿到对应的cell TableViewCell *cell = (TableViewCell *)[self.mainTableView cellForRowAtIndexPath:indexPath]; // 判断这个 cell 是否正在显示 NSArray *visibleCellArray = [self.mainTableView visibleCells]; if ([visibleCellArray containsObject:cell]) { cell.pictureImageView.image = image; } // 移除观察者(modal 通知控制器不要在继续观察) [object removeObserver:self forKeyPath:@"image"]; }
// 给 modal 注册观察者 // context 传出的 indexpath 是为了在观察者的回调方法里面拿到对应的cellint [model addObserver:self forKeyPath:@"image" options:(NSKeyValueObservingOptionNew) context:(__bridge_retained void *)(indexPath)]; [cell setCellModel:model];
相关文章推荐
- 贝塞尔曲线
- String对象
- [LintCode] Find the Weak Connected Component in the Directed Graph
- [Leetcode] Different Ways to Add Parentheses
- VIM配置笔记(cscope+NERDTree)
- Safari的常用快捷键
- 我的程序员未婚夫
- 【UVA796】【无向图割边】
- Windows 端口和任务 查看 相关命令
- CRgn::CreateEllipticRgnIn
- 第三方加载图片
- 获取本机外网ip获取位置
- 散列操作的基本例程(1)
- react-native试玩(6)-日期选择控件
- hdu 2002 计算球体积
- oracle 学习sql
- OC03_继承
- leetcode 201:Bitwise AND of Numbers Range
- 计算机为什么要用补码
- Java核心API -- 14(Socket)