前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UITableViewCell自适应网络不规则图片和文字组合的高度

UITableViewCell自适应网络不规则图片和文字组合的高度

作者头像
Python疯子
发布2018-09-06 16:08:29
2K0
发布2018-09-06 16:08:29
举报
文章被收录于专栏:Python疯子Python疯子

列表样式

有时我们会需要对cell的图片和文字进行显示并完美自适配其大小,下面用我有限的知识做了个适配,看着好像还能用,哈哈 直接上code 001 在tableview的获取cell高度的方法里写调用自定义cell的一个方法

代码语言:javascript
复制
  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 计算cell内容的高度 
TableViewCell *cell = (TableViewCell *)[self tableView:_tableView cellForRowAtIndexPath:indexPath];
return [cell cellForHeight];
   }

002 接下来开始重点喽 自定义TableViewCell的.h文件, 做主要控件

代码语言:javascript
复制
@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];

代码语言:javascript
复制
// 给文字赋值
[self setreviewContentText:model.describe];
}

003__01 文字的自适应高度

代码语言:javascript
复制
//赋值 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

代码语言:javascript
复制
 -(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;
}
}

图片不存在,下载图片

代码语言:javascript
复制
  - (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 在列表页收到刷新通知,并刷新列表

代码语言:javascript
复制
 // 接受通知并刷新tableview
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reload:) name:@"reload" object:nil];

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

到此就欧了

1135.gif

点击下载Demo

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.12.30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档