图片下载---KVO
2015-09-28 20:04
176 查看
@interface
RootTableViewController ()
#pragma mark 数据放在容器内
@property(nonatomic,strong)
NSMutableArray *array;
@end
@implementation RootTableViewController
#pragma mark 重用标识符
static
NSString * const cell_ID =
@"MyCellID";
- (void)viewDidLoad {
[super
viewDidLoad];
//1.
NSURLRequest *request = [[NSURLRequest
alloc]initWithURL:[NSURL
URLWithString:BASE_URL]];
[NSURLConnection
sendAsynchronousRequest:request
queue:[NSOperationQueue
mainQueue] completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError) {
NSDictionary *dictionary = [NSJSONSerialization
JSONObjectWithData:data options:NSJSONReadingAllowFragments
error:nil];
_array = [[NSMutableArray
alloc]initWithCapacity:16];
for (NSDictionary *dict
in dictionary[@"news"]) {
News *news = [News
new];
[news setValuesForKeysWithDictionary:dict];
[_array
addObject:news];
}
//请求完数据后,要刷新--如果是其他视图,就写一个方法,把数据传出去,更新视图
[self.tableView
reloadData];
}];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_array
count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {
MyCell *cell = [tableView
dequeueReusableCellWithIdentifier:cell_ID
forIndexPath:indexPath];
//取出Model对象
News *news = _array[indexPath.row];
//如果这个Model对象的图片为空,并且下载状态为NO。这种情况属于第一次运行的时候。
if (news.newsImage ==
nil && news.isDownloading ==
NO) {
//先显示默认静态图片
cell.imgView.image = [UIImage
imageNamed:@"background.png"];
//去下载图片
[news
loadImage];
//添加观察者
[news addObserver:self
forKeyPath:@"newsImage"
options:NSKeyValueObservingOptionNew
context:(__bridge
void *)(indexPath)];
}else{
if (news.newsImage ==
nil) {
cell.imgView.image = [UIImage
imageNamed:@"background.png"];
}else{
cell.imgView.image = news.newsImage;
}
}
return cell;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary
*)change context:(void *)context
{
//做项目的小技巧:强转,字典的value值是id型。
UIImage *image = (UIImage *)change[@"new"];
//图片没有下载完就一直等着。
if (image == nil) {
return;
}
//取出可视区域cell所在位置的集合
NSArray *array = [self.tableView
indexPathsForVisibleRows];
//获取被观察的Model对象对应赋值的cell
NSIndexPath *indexPath = (__bridge
NSIndexPath*)(context);
//判断被观察的cell是否显示在可视区域
if ([array containsObject:indexPath]) {
//如果在显示区域,我们取出对应的cell,更新数据
MyCell *cell = (MyCell *) [self.tableView
cellForRowAtIndexPath:indexPath];
cell.imgView.image = image;
//更新cell
[self.tableView
reloadRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
[object removeObserver:self
forKeyPath:@"newsImage"];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
return 300;
}
RootTableViewController ()
#pragma mark 数据放在容器内
@property(nonatomic,strong)
NSMutableArray *array;
@end
@implementation RootTableViewController
#pragma mark 重用标识符
static
NSString * const cell_ID =
@"MyCellID";
- (void)viewDidLoad {
[super
viewDidLoad];
//1.
NSURLRequest *request = [[NSURLRequest
alloc]initWithURL:[NSURL
URLWithString:BASE_URL]];
[NSURLConnection
sendAsynchronousRequest:request
queue:[NSOperationQueue
mainQueue] completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError) {
NSDictionary *dictionary = [NSJSONSerialization
JSONObjectWithData:data options:NSJSONReadingAllowFragments
error:nil];
_array = [[NSMutableArray
alloc]initWithCapacity:16];
for (NSDictionary *dict
in dictionary[@"news"]) {
News *news = [News
new];
[news setValuesForKeysWithDictionary:dict];
[_array
addObject:news];
}
//请求完数据后,要刷新--如果是其他视图,就写一个方法,把数据传出去,更新视图
[self.tableView
reloadData];
}];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_array
count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {
MyCell *cell = [tableView
dequeueReusableCellWithIdentifier:cell_ID
forIndexPath:indexPath];
//取出Model对象
News *news = _array[indexPath.row];
//如果这个Model对象的图片为空,并且下载状态为NO。这种情况属于第一次运行的时候。
if (news.newsImage ==
nil && news.isDownloading ==
NO) {
//先显示默认静态图片
cell.imgView.image = [UIImage
imageNamed:@"background.png"];
//去下载图片
[news
loadImage];
//添加观察者
[news addObserver:self
forKeyPath:@"newsImage"
options:NSKeyValueObservingOptionNew
context:(__bridge
void *)(indexPath)];
}else{
if (news.newsImage ==
nil) {
cell.imgView.image = [UIImage
imageNamed:@"background.png"];
}else{
cell.imgView.image = news.newsImage;
}
}
return cell;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary
*)change context:(void *)context
{
//做项目的小技巧:强转,字典的value值是id型。
UIImage *image = (UIImage *)change[@"new"];
//图片没有下载完就一直等着。
if (image == nil) {
return;
}
//取出可视区域cell所在位置的集合
NSArray *array = [self.tableView
indexPathsForVisibleRows];
//获取被观察的Model对象对应赋值的cell
NSIndexPath *indexPath = (__bridge
NSIndexPath*)(context);
//判断被观察的cell是否显示在可视区域
if ([array containsObject:indexPath]) {
//如果在显示区域,我们取出对应的cell,更新数据
MyCell *cell = (MyCell *) [self.tableView
cellForRowAtIndexPath:indexPath];
cell.imgView.image = image;
//更新cell
[self.tableView
reloadRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
[object removeObserver:self
forKeyPath:@"newsImage"];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
return 300;
}
相关文章推荐
- zoj 1610 Count the Colors 线段树区间更新——染色问题
- Jmeter分布式测试
- hdu 5491(位运算)
- hdu 5491(位运算)
- 获取ios设备型号
- hdu 5492(dp)
- 第四周项目一 建立单链表
- 图片封装下载器--自己的代码库--可以直接使用
- hdu5475__An easy problem(线段树单点更新)
- How to Write an Effective Design Document
- 3 B. Lorry
- ZOJ第1240题 灰常简单的题目
- canvas鼠标点击散播图片
- 代码调试中偶然错误的处理方法
- 文本框 UITextView
- 【读书笔记】iOS-KVC
- 固定列表标题栏
- POJ 1845 Sumdiv (数学,乘法逆元)
- poj3320 尺取法~~
- Python进阶 - HTML获取与解析