UITableViewCell加载图片
2015-10-04 20:27
711 查看
数据模型
.h文件
//图片属性
@property(nonatomic,strong) UIImage *newsImage;
//图片下载状态
@property(nonatomic,assign) BOOL isDownLoading;
//下载图片的方法
-(void)loadImage;
@property(nonatomic,assign) NSInteger rowNum;
@property(nonatomic,strong) NSString *Id;
@property(nonatomic,strong) NSString *title;
@property(nonatomic,strong) NSString *type;
@property(nonatomic,strong) NSString *cid;
@property(nonatomic,strong) NSString *cname;
@property(nonatomic,strong) NSString *newsUrl;
@property(nonatomic,strong) NSString *typeId;
@property(nonatomic,assign) NSInteger sequence;
@property(nonatomic,assign) NSInteger attribute;
@property(nonatomic,strong) NSString *lastUpdateTime;
@property(nonatomic,strong) NSString *PUBLISHDATE;
@property(nonatomic,strong) NSString *picUrl;
@property(nonatomic,strong) NSString *summary;
@property(nonatomic,assign) NSInteger commentCount;
@property(nonatomic,strong) NSString *newsId;
.m文件
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
//占用系统关键字的key值需要重新指定映射
if ([key isEqualToString:@"id"]) {
_Id = value;
NSLog(@"%@",key);
}
}
//此方法适用于我们调试使用(debug模式下),检验model数据,直观的观察到model的值(默认打印的是地址)
- (NSString *)description
{
return [NSString stringWithFormat:@"rowNum=%ld,Id=%@,title=%@,type=%@,cid=%@,cname=%@,newsUrl=%@,typeId=%@,sequence=%ld,attribute=%ld,lastUpdateTime=%@,PUBLISHDATE=%@,picUrl=%@,summary=%@,commentCount=%ld",_rowNum,_Id,_title,_type,_cid,_cname,_newsUrl,_typeId,_sequence,_attribute,_lastUpdateTime,_PUBLISHDATE,_picUrl,_summary
,_commentCount];
}
//下载图片的方法
-(void)loadImage
{
[ZQImageDownLoader iamgeDownLoad:_picUrl block:^(UIImage *image) {
_newsImage = image;
//表示加载完成
_isDownLoading = NO;
}];
//先走这个方法,表示正在加载
_isDownLoading = YES;
}
方法一:SDWebImage第三方
#warning 方法一:使用第三方
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:news.picUrl] placeholderImage:[UIImage imageNamed:@"image0.jpg"]];
方法二:KVO
//第一次运行时,图片为空并且还未加载
// if(news.newsImage == nil && news.isDownLoading == NO)
// {
// //则显示一张默认图片
// cell.imageView.image = [UIImage imageNamed:@"image0.jpg"];
//
// //加载图片
// [news loadImage];
//
// //增加观察者
// [news addObserver:self forKeyPath:@"newsImage" options:NSKeyValueObservingOptionNew context:(__bridge void*)indexPath];
// }
// else{
// //当图片正在加载
// if(news.newsImage == nil)
// {
// cell.imageView.image = [UIImage imageNamed:@"image0.jpg"];
//
// }
// else{
//
// cell.imageView.image = news.newsImage;
// }
// }
//
// return cell;
//}
//
//
//
//-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
//{
// //字典通过键值找到的值是id类型,在做项目期间,要注意写强转
// UIImage *image = (UIImage *)(change[@"new"]);
//
// if (image == nil) {
// return;
// }
//
// //取出可视区域cell所在的位置的集合
// NSArray *array = [self.tableView indexPathsForVisibleRows];
// //获取被观察的model对象
// NSIndexPath *indexPath = (__bridge NSIndexPath *)(context);
// //判断被观察的对象是否显示在可视区域
// if ([array containsObject:indexPath]) {
// //如果在显示区域,我们取出对应的cell更新数据
// MyCell *cell = (MyCell *)[self.tableView cellForRowAtIndexPath:indexPath];
// cell.imageView.image = image;
// //更新cell
// [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// }
//
// //移除观察者
// [object removeObserver:self forKeyPath:@"newsImage"];
//
//
//}
.h文件
//图片属性
@property(nonatomic,strong) UIImage *newsImage;
//图片下载状态
@property(nonatomic,assign) BOOL isDownLoading;
//下载图片的方法
-(void)loadImage;
@property(nonatomic,assign) NSInteger rowNum;
@property(nonatomic,strong) NSString *Id;
@property(nonatomic,strong) NSString *title;
@property(nonatomic,strong) NSString *type;
@property(nonatomic,strong) NSString *cid;
@property(nonatomic,strong) NSString *cname;
@property(nonatomic,strong) NSString *newsUrl;
@property(nonatomic,strong) NSString *typeId;
@property(nonatomic,assign) NSInteger sequence;
@property(nonatomic,assign) NSInteger attribute;
@property(nonatomic,strong) NSString *lastUpdateTime;
@property(nonatomic,strong) NSString *PUBLISHDATE;
@property(nonatomic,strong) NSString *picUrl;
@property(nonatomic,strong) NSString *summary;
@property(nonatomic,assign) NSInteger commentCount;
@property(nonatomic,strong) NSString *newsId;
.m文件
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
//占用系统关键字的key值需要重新指定映射
if ([key isEqualToString:@"id"]) {
_Id = value;
NSLog(@"%@",key);
}
}
//此方法适用于我们调试使用(debug模式下),检验model数据,直观的观察到model的值(默认打印的是地址)
- (NSString *)description
{
return [NSString stringWithFormat:@"rowNum=%ld,Id=%@,title=%@,type=%@,cid=%@,cname=%@,newsUrl=%@,typeId=%@,sequence=%ld,attribute=%ld,lastUpdateTime=%@,PUBLISHDATE=%@,picUrl=%@,summary=%@,commentCount=%ld",_rowNum,_Id,_title,_type,_cid,_cname,_newsUrl,_typeId,_sequence,_attribute,_lastUpdateTime,_PUBLISHDATE,_picUrl,_summary
,_commentCount];
}
//下载图片的方法
-(void)loadImage
{
[ZQImageDownLoader iamgeDownLoad:_picUrl block:^(UIImage *image) {
_newsImage = image;
//表示加载完成
_isDownLoading = NO;
}];
//先走这个方法,表示正在加载
_isDownLoading = YES;
}
方法一:SDWebImage第三方
#warning 方法一:使用第三方
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:news.picUrl] placeholderImage:[UIImage imageNamed:@"image0.jpg"]];
方法二:KVO
//第一次运行时,图片为空并且还未加载
// if(news.newsImage == nil && news.isDownLoading == NO)
// {
// //则显示一张默认图片
// cell.imageView.image = [UIImage imageNamed:@"image0.jpg"];
//
// //加载图片
// [news loadImage];
//
// //增加观察者
// [news addObserver:self forKeyPath:@"newsImage" options:NSKeyValueObservingOptionNew context:(__bridge void*)indexPath];
// }
// else{
// //当图片正在加载
// if(news.newsImage == nil)
// {
// cell.imageView.image = [UIImage imageNamed:@"image0.jpg"];
//
// }
// else{
//
// cell.imageView.image = news.newsImage;
// }
// }
//
// return cell;
//}
//
//
//
//-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
//{
// //字典通过键值找到的值是id类型,在做项目期间,要注意写强转
// UIImage *image = (UIImage *)(change[@"new"]);
//
// if (image == nil) {
// return;
// }
//
// //取出可视区域cell所在的位置的集合
// NSArray *array = [self.tableView indexPathsForVisibleRows];
// //获取被观察的model对象
// NSIndexPath *indexPath = (__bridge NSIndexPath *)(context);
// //判断被观察的对象是否显示在可视区域
// if ([array containsObject:indexPath]) {
// //如果在显示区域,我们取出对应的cell更新数据
// MyCell *cell = (MyCell *)[self.tableView cellForRowAtIndexPath:indexPath];
// cell.imageView.image = image;
// //更新cell
// [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// }
//
// //移除观察者
// [object removeObserver:self forKeyPath:@"newsImage"];
//
//
//}
相关文章推荐
- Servlet的学习之Request请求对象(2)
- ios-UI常见问题之TableView异步加载图片错乱显示
- The JSP specification requires that an attribute name is preceded by whitespace
- SPOJ 1771 NQUEEN (DLX解n皇后问题)
- POST和GET、NSMutableURLRequest和NSURLRequest两组分别的区别
- ZOJ2587 Unique Attack(判定最小割唯一性)
- TextVeiw 的 No package identifier when getting value for resource numb
- Servlet的学习之Request请求对象(1)
- UI课程11 自定义cell及自适应高度
- codeforces 163A A. Substring and Subsequence(dp)
- UIPickerView控件的使用
- 63. Unique Paths II (Graph; DP)
- 62. Unique Paths (Graph; DP)
- UI 过后的杂谈
- SpriteBuilder修改CCB文件中的子CCB文件需要注意的一个地方
- SpriteBuilder修改CCB文件中的子CCB文件需要注意的一个地方
- SpriteBuilder修改CCB文件中的子CCB文件需要注意的一个地方
- String,StringBuffer与StringBuilder的区别??
- 126. Word Ladder II(Map, set, adj, Queue; BFS)
- UVa12166 Equilibrium Mobile(修改天平)