您的位置:首页 > 其它

图片下载---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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: