UITableViewCell加载图片

来源:互联网 发布:西门子plc300编程软件 编辑:程序博客网 时间:2024/05/27 08:13

数据模型

.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"];
//    
//    
//}


0 0
原创粉丝点击