OC TableView根据内容获取高度 1

#import "MeCustomCell.h"

@interface NewsDetailCell : MeCustomCell

@property (nonatomic, copy) void (^tableViewCellH)(CGFloat cellHeight);

- (void)makeSize;

@end

makeSize方法

- (void) makeSize {
   CGFloat cellWidth = kScreen_Width - leftPadding * 2;
    self.detailLab.frame = CGRectMake(leftPadding, leftPadding*2, cellWidth, 20);
    self.titleLab.frame = CGRectMake(leftPadding, self.detailLab.bottom + leftPadding *3, cellWidth, 0);
    [self.titleLab sizeToFit];
    [self.imgView sizeToFit];
    CGSize newSize = self.imgView.size;
    if (newSize.width > cellWidth) {
        newSize = CGSizeMake(cellWidth, cellWidth * newSize.height/newSize.width);
    }
    self.imgView.frame = CGRectMake(leftPadding,  self.titleLab.bottom , newSize.width, newSize.height);
    self.bgView.frame = CGRectMake(leftPadding /2, self.titleLab.top - leftPadding, kScreen_Width - leftPadding, self.titleLab.height + self.imgView.height + leftPadding * 2);
    if (self.tableViewCellH) {
        self.tableViewCellH(self.bgView.bottom);
    }
}

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString *identifier = @"NewsDetailCellID";
    NewsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[NewsDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.tableViewCellH = ^(CGFloat cellHeight) {
            self.cellHeight = cellHeight;
        };
    }
    cell.detailLab.text = @"8月8日周三下午4:12";
    cell.titleLab.text = _dataAry[indexPath.row];
    NSURL *url= [NSURL URLWithString:_dataAry[indexPath.row]];
    if (url) {
        [cell.imgView sd_setImageWithURL:url];
    } else {
        cell.imgView.image = nil;
    }
      [cell makeSize];//数据加载后才能计算高度
    return cell;
  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return _cellHeight?:100;

你可能感兴趣的:(OC TableView根据内容获取高度 1)