tableViewCell自适应网络图片行高
==========LJFImageDetailCollection.h=========
//
LJFImageDetailCollection.h
#import
@interfaceLJFImageDetailTableContr :UITableViewController
@property(nonatomic,strong)NSArray* imagesArray;
@end
========LJFImageDetailCollection.m========
#define PRODUCTDETAIL @"http://59.110.64.56/liangpin/upload/goods/album/large/detail_"
#import"LJFImageDetailTableContr.h"
#import"LJFTableViewCell.h"
@interfaceLJFImageDetailTableContr()
@property(nonatomic,strong)NSMutableDictionary*
dictImag ;
@property(nonatomic,strong)NSMutableDictionary*
dictH ;
@property(nonatomic,strong)NSMutableArray*
imagearry ;
@end
@implementationLJFImageDetailTableContr
staticNSString*constreuseIdentifier =@"Cell";
- (void)viewDidLoad {
[superviewDidLoad];
[self.tableViewregisterClass:[LJFTableViewCellclass]forCellReuseIdentifier:@"cell"];
self.tableView.showsVerticalScrollIndicator=NO;
self.tableView.showsHorizontalScrollIndicator=NO;
self.dictImag= [NSMutableDictionarydictionary];
self.dictH= [NSMutableDictionarydictionary];
self.imagearry= [NSMutableArrayarray];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
}
-(void)setImagesArray:(NSArray*)imagesArray{
_imagesArray= imagesArray;
__blocktypeof(self) weakSelf =self;
for(inti =0;i < imagesArray.count; i++ ){
NSURL* url = [NSURLURLWithString:[NSStringstringWithFormat:@"%@%@",PRODUCTDETAIL,self.imagesArray[i]]];
UIImageView* imageView = [[UIImageViewalloc]init];
[self.imagearryaddObject:imageView];
[imageViewsd_setImageWithURL:urlcompleted:^(UIImage*image,NSError*error,SDImageCacheTypecacheType,NSURL*imageURL) {
NSString* urlStr = [imageURLabsoluteString];
[weakSelf.dictImagsetObject:imageforKey:urlStr];
CGFloatheight = [UIScreenmainScreen].bounds.size.width/ (image.size.width/image.size.height);
NSString* heiStr = [NSStringstringWithFormat:@"%f",height];
[weakSelf.dictHsetObject:heiStrforKey:urlStr];
[weakSelf.tableViewreloadData];
}];
}
}
#pragma
mark
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
returnself.imagesArray.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
NSString* urlStr = [NSStringstringWithFormat:@"%@%@",PRODUCTDETAIL,self.imagesArray[indexPath.item]];
LJFTableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:@"cell"];
cell.imageView.image= [self.dictImagobjectForKey:urlStr];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
returncell;
}
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
NSString* urlStr = [NSStringstringWithFormat:@"%@%@",PRODUCTDETAIL,self.imagesArray[indexPath.item]];
NSString* heightStr = [self.dictHobjectForKey:urlStr];
CGFloatheight = [heightStrfloatValue];
if(height >10) {
returnheight;
}else{
return10;
}
}
@end
======LJFCollectionViewCell.h======
#import
#import"LJFTableImageView.h"
@interfaceLJFTableViewCell :UITableViewCell
@property(nonatomic,strong)LJFTableImageView*
imageV;
@end
========LJFCollectionViewCell.m ==========
//
LJFCollectionViewCell.m
#import"LJFTableViewCell.h"
@interfaceLJFTableViewCell()
@end
@implementationLJFTableViewCell
-(instancetype)initWithFrame:(CGRect)frame{
self= [superinitWithFrame:frame];
if(self) {
for(UIView* viewinself.subviews) {
[viewremoveFromSuperview];
}
[selfaddSubview:self.imageV];
}
returnself;
}
-(void)layoutSubviews{
[superlayoutSubviews];
self.imageView.width= [UIScreenmainScreen].bounds.size.width-40;
self.imageView.height=self.height-10;
}
-(LJFTableImageView*)imageV{
if(_imageV==nil) {
_imageV= [[LJFTableImageViewalloc]init];
}
return_imageV;
}
@end
========LJFTableImageView.m==========
#import"LJFTableImageView.h"
@interfaceLJFTableImageView()
@end
@implementationLJFTableImageView
-(void)layoutSubviews{
[superlayoutSubviews];
self.contentMode=UIViewContentModeScaleAspectFill;
self.clipsToBounds=YES;
}
@end