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

wbdy4196 7年前
   <p><img src="https://simg.open-open.com/show/3cac4ecb832d2c1720f6afb052ec30ad.png"></p>    <p>列表样式</p>    <p>有时我们会需要对cell的图片和文字进行显示并完美自适配其大小,下面用我有限的知识做了个适配,看着好像还能用,哈哈</p>    <p>直接上code</p>    <p>001 在tableview的获取cell高度的方法里写调用自定义cell的一个方法</p>    <pre>  <code class="language-objectivec">- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  {  // 计算cell内容的高度   TableViewCell *cell = (TableViewCell *)[self tableView:_tableView cellForRowAtIndexPath:indexPath];  return [cell cellForHeight];     }</code></pre>    <p>002 接下来开始重点喽</p>    <p>自定义TableViewCell的.h文件, 做主要控件</p>    <pre>  <code class="language-objectivec">@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</code></pre>    <p>003 在.m文件里进行赋值</p>    <pre>  <code class="language-objectivec">- (void)setModel:(DataModel *)model  {  self.title.text = model.title;  // 给图片赋值  [self setImageURLSize:model.imageURL];    // 给文字赋值  [self setreviewContentText:model.describe];  }</code></pre>    <p>003__01 文字的自适应高度</p>    <pre>  <code class="language-objectivec">//赋值 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;  }</code></pre>    <p>003__02 网络不规则图片的自适应高度,记得导入SDWebImage</p>    <pre>  <code class="language-objectivec">-(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;  }  }</code></pre>    <p>图片不存在,下载图片</p>    <pre>  <code class="language-objectivec">- (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];                    });  }];    }</code></pre>    <p>004 在列表页收到刷新通知,并刷新列表</p>    <pre>  <code class="language-objectivec">// 接受通知并刷新tableview  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reload:) name:@"reload" object:nil];    - (void)reload:(UIButton *)button  {   [_tableView reloadData];  }</code></pre>    <p>到此就欧了</p>    <p><img src="https://simg.open-open.com/show/31c425479aac227ee61fd9d03d6933a0.gif"></p>    <p>1135.gif</p>    <p><a href="/misc/goto?guid=4959732717769784877" rel="nofollow,noindex"><strong>点击下载Demo</strong> </a></p>    <p> </p>    <p>来自:http://www.jianshu.com/p/d010949b40b4</p>    <p> </p>