UITableViewCell自適應網絡不規則圖片和文字組合的高度

wbdy4196 7年前發布 | 22K 次閱讀 iOS開發 移動開發 UITableViewCell

列表樣式

有時我們會需要對cell的圖片和文字進行顯示并完美自適配其大小,下面用我有限的知識做了個適配,看著好像還能用,哈哈

直接上code

001 在tableview的獲取cell高度的方法里寫調用自定義cell的一個方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 計算cell內容的高度 
TableViewCell *cell = (TableViewCell *)[self tableView:_tableView cellForRowAtIndexPath:indexPath];
return [cell cellForHeight];
   }

002 接下來開始重點嘍

自定義TableViewCell的.h文件, 做主要控件

@interface TableViewCell : UITableViewCell

@property (nonatomic, strong) UILabel *title;
@property (nonatomic, strong) UIImageView *photo;
@property (nonatomic, strong) UILabel *describe;

@property (nonatomic, assign) CGSize imageSize;
@property (nonatomic, assign) CGSize describeSize;

@property (nonatomic, strong) DataModel *model;

- (void)setModel:(DataModel *)model;

 // 獲取cell的高度的方法
- (CGFloat)cellForHeight;
@end

003 在.m文件里進行賦值

- (void)setModel:(DataModel *)model
{
self.title.text = model.title;
// 給圖片賦值
[self setImageURLSize:model.imageURL];

// 給文字賦值
[self setreviewContentText:model.describe];
}

003__01 文字的自適應高度

//賦值 and 自動換行,計算出cell的高度
-(void)setreviewContentText:(NSString*)text
{
//獲得當前cell高度
CGRect frame = [self frame];
//文本賦值
self.describe.text = text;

//設置label的最大行數
self.describe.numberOfLines = 0;
CGSize size = CGSizeMake(self.width-30, 1000);
self.describeSize = [self.describe.text sizeWithFont:self.describe.font constrainedToSize:size lineBreakMode:NSLineBreakByClipping];
self.describe.frame = CGRectMake(self.describe.frame.origin.x, self.photo.bottom + 10, _describe.width, _describeSize.height);
frame.size.height = _describe.height;

self.frame = frame;
}

003__02 網絡不規則圖片的自適應高度,記得導入SDWebImage

-(void)setImageURLSize:(NSString*)imageURL
{
// 先從緩存中查找圖片
UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey: imageURL];

// 沒有找到已下載的圖片就使用默認的占位圖,當然高度也是默認的高度了,除了高度不固定的文字部分。
if (!image) {
    image = [UIImage imageNamed:@"Wechat"];
//  圖片不存在,下載圖片
    [self downloadImage:imageURL];
}
else
{
    self.photo.image = image;
    //手動計算cell
    CGFloat imgHeight = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;
    _photo.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width , imgHeight);
    _imageSize.height = imgHeight;
}
}

圖片不存在,下載圖片

- (void)downloadImage:(NSString*)imageURL
 {
// 利用 SDWebImage 框架提供的功能下載圖片
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageURL] options:(SDWebImageDownloaderUseNSURLCache) progress:^(NSInteger receivedSize, NSInteger expectedSize) {

} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
    [[SDImageCache sharedImageCache] storeImage:image forKey:imageURL toDisk:YES];

    dispatch_async(dispatch_get_main_queue(), ^{
                // 回到主線程做操做
        // 請求完成 刷新代碼
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reload" object:nil];

                });
}];

}

004 在列表頁收到刷新通知,并刷新列表

// 接受通知并刷新tableview
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reload:) name:@"reload" object:nil];

- (void)reload:(UIButton *)button
{
 [_tableView reloadData];
}

到此就歐了

1135.gif

點擊下載Demo

 

來自:http://www.jianshu.com/p/d010949b40b4

 

 本文由用戶 wbdy4196 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!