ios 进阶#iOS#HeminWon恩美第二个APP项目

计算tableView不等高cell高度的几种方法

2016-04-13  本文已影响1467人  OneAlon

这里利用heightForRowAtIndexPath:方法计算不等高cell的高度,在使用这个方法之前要明确这个方法的调用时间以及调用次数:

//返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight;
}

计算不等高cell高度的第一种方法:估算高度

(void)viewDidLoad {
    [super viewDidLoad];
    //设置估算高度
    self.tableView.estimatedRowHeight = 170;
}
//定义间距
#define WYLMargin 10
//返回cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取对应的模型
    WYLTopicItem *item = self.topicItemArray[indexPath.row];
    
    //定义cell的高度
    CGFloat cellHeight = 0;
    
    //图片+间距
    cellHeight += WYLMargin + 35 + WYLMargin;
    
    //label的高度
    CGSize textMaxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 * WYLMargin, MAXFLOAT);
    CGFloat labelHeight = [item.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
    cellHeight += labelHeight + WYLMargin;
    
    //底部导航栏的高度
    cellHeight += 35 + WYLMargin;
    NSLog(@"heightForRowAtIndexPath-----%zd",indexPath.row);
    
    return cellHeight;
}

计算不等高cell高度的第二种方法:定义字典保存cell高度

/** 存储cell高度的字典*/
@property (nonatomic ,strong) NSMutableDictionary *cellHeightDict;
/** 懒加载存储cell高度的字典*/
-(NSMutableDictionary *)cellHeightDict
{
    if (_cellHeightDict == nil) {
        _cellHeightDict = [NSMutableDictionary dictionary];
    }
    
    return _cellHeightDict;
}
//返回cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取对应的模型
    WYLTopicItem *item = self.topicItemArray[indexPath.row];
    
    //用模型的地址作为字典的key
    NSString *key = [NSString stringWithFormat:@"%p",item];
    
    //取出模型对应的cell的高度
    CGFloat cellHeight = [self.cellHeightDict[key] doubleValue];
    
    //如果cellHeight有值,就是cell的高度已经计算过了,直接返回
    if (cellHeight) return cellHeight;
    
    //如果cell的高度没有计算过,就计算cell高度
    //图片+间距
    cellHeight += WYLMargin + 35 + WYLMargin;
    
    //label的高度
    CGSize textMaxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 * WYLMargin, MAXFLOAT);
    CGFloat labelHeight = [item.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
    cellHeight += labelHeight + WYLMargin;
    
    //底部导航栏的高度
    cellHeight += 35 + WYLMargin;

    //将计算过的cell高度保存到字典中
    self.cellHeightDict[key] = @(cellHeight);
    NSLog(@"heightForRowAtIndexPath----%zd",indexPath.row);
    
    return cellHeight;
}

计算不等高cell高度的第三种方法:在模型中定义cellHeight属性

#import <Foundation/Foundation.h>
@interface WYLTopicItem : NSObject
/** 根据当前模型数据计算出来的cell高度*/
@property (nonatomic ,assign) NSInteger cellHeight;
@end
#import "WYLTopicItem.h"
@implementation WYLTopicItem
-(NSInteger)cellHeight
{
    //如果cellHeight已经计算过,就直接返回
    if (_cellHeight) return _cellHeight;
 
    //图片+间距
    _cellHeight += WYLMargin + 35 + WYLMargin;
    
    //label的高度
    CGSize textMaxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 * WYLMargin, MAXFLOAT);
    CGFloat labelHeight = [self.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
    _cellHeight += labelHeight + WYLMargin;
    
    //底部导航栏的高度
    _cellHeight += 35 + WYLMargin;
    
    return _cellHeight;
}
@end

-3.在heightForRowAtIndexPath:方法中计算cell的高度

//返回cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取对应的模型
    WYLTopicItem *item = self.topicItemArray[indexPath.row];
    return item.cellHeight;
}

效果图.png
上一篇 下一篇

猜你喜欢

热点阅读