美文网首页
Cell 高度缓存

Cell 高度缓存

作者: ziyouzhe4 | 来源:发表于2017-04-19 15:30 被阅读19次

    把之前做的demo放在这里,以后方便看

    cell代码 .h

    #import <UIKit/UIKit.h>
    #import "DDModel.h"
    
    @interface DDCell : UITableViewCell
    
    @property (nonatomic,strong)DDModel *model;
    
    + (instancetype)cellWithTableView:(UITableView *)tableView;
    
    @end
    
    

    cell代码 .m

    #import "DDCell.h"
    
    @interface DDCell()
    
    @property (nonatomic,strong)UILabel *first;
    @property (nonatomic,strong)UILabel *second;
    @property (nonatomic,strong)UILabel *third;
    
    @end
    
    @implementation DDCell
    
    + (instancetype)cellWithTableView:(UITableView *)tableView{
        
        static NSString *identifier = @"DDCell";
             // 1.缓存中取
         DDCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
             // 2.创建
         if (cell == nil) {
            cell = [[DDCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        return cell;
        
    }
    
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
        
        if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
            [self setupSubviews];
        }
        return self;
    }
    
    - (void)setupSubviews{
        
        //创建第一个label
        self.first = [[UILabel alloc] init];
        self.second = [[UILabel alloc] init];
        self.third = [[UILabel alloc] init];
        
        self.first.numberOfLines = 0;
        self.second.numberOfLines = 0;
        self.third.numberOfLines = 0;
        
    //    self.first.backgroundColor = [UIColor redColor];
    //    self.second.backgroundColor = [UIColor blueColor];
    //    self.third.backgroundColor = [UIColor greenColor];
        
        [self addSubview:self.first];
        [self addSubview:self.second];
        [self addSubview:self.third];
        
    }
    
    - (void)setModel:(DDModel *)model{
        
        CGFloat margin = 8;
        CGFloat left = 12;
        CGFloat width = [UIScreen mainScreen].bounds.size.width - 2 * left;
        _model = model;
        //计算所用高度,设置frame
            
        // 1111111111
        self.first.text = model.firstContent;
        
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:model.firstContent];
        UIFont *font = [UIFont systemFontOfSize:18];
        UIColor *color = [UIColor redColor];
        NSRange allRange = [model.firstContent rangeOfString:model.firstContent];
        [attrStr addAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color} range:allRange];
        
        CGFloat fx = left;
        CGFloat fy = 0;
        
        CGFloat fw = width;
        
        CGSize size = [attrStr boundingRectWithSize:CGSizeMake(fw, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
        self.first.frame = CGRectMake(fx, fy, fw, size.height);
        // 22222
        self.second.text = model.secondContent;
        NSMutableAttributedString *attrStr2 = [[NSMutableAttributedString alloc] initWithString:model.secondContent];
        NSRange allRange2 = [model.secondContent rangeOfString:model.secondContent];
        [attrStr2 addAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color} range:allRange2];
    
        CGFloat sx = left;
        CGFloat sy = fy + size.height + margin;
        CGSize size2 = [attrStr2 boundingRectWithSize:CGSizeMake(fw, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
        self.second.frame = CGRectMake(sx, sy, fw, size2.height);
    
        // 33333333
    
        self.third.text = model.thirdContent;
        NSMutableAttributedString *attrStr3 = [[NSMutableAttributedString alloc] initWithString:model.thirdContent];
        NSRange allRange3 = [model.secondContent rangeOfString:model.thirdContent];
        [attrStr3 addAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color} range:allRange3];
        
        CGFloat tx = left;
        CGFloat ty = sy + size2.height + margin;
        
        CGSize size3 = [attrStr3 boundingRectWithSize:CGSizeMake(fw, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
        
        self.third.frame = CGRectMake(tx, ty, fw, size3.height);
        model.cellHeight = size3.height + ty + margin;
        
    }
    
    

    模型 .h

    #import <UIKit/UIKit.h>
    
    @interface DDModel : NSObject
    @property (nonatomic,strong)NSString *firstContent;
    @property (nonatomic,strong)NSString *secondContent;
    @property (nonatomic,strong)NSString *thirdContent;
    //记录cell高度
    @property (nonatomic,assign)CGFloat cellHeight;
    
    @end
    

    Controller 中用法

    
    #import "ViewController.h"
    #import "DDModel.h"
    #import "DDCell.h"
    
    
    
    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
    
    @property (nonatomic,strong)UITableView *tableView;
    @property (nonatomic,strong)NSMutableArray *dataArray;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.dataArray = [NSMutableArray arrayWithCapacity:100];
    
        for (int i = 0; i < 100; i++) {
            
            DDModel *model = [[DDModel alloc] init];
            model.firstContent = @"11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
            model.secondContent = @"2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222";
            model.thirdContent = @"3333333333333333333";
            
            [self.dataArray addObject:model];
            
        }
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        
        [self.view addSubview:self.tableView];
        
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        DDCell *cell = [DDCell cellWithTableView:tableView];
      
        cell.model = self.dataArray[indexPath.row];
        
        return  cell;
        
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return  self.dataArray.count;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        DDModel *model = self.dataArray[indexPath.row];
        return  model.cellHeight;
        
    }
    
    
    

    相关文章

      网友评论

          本文标题:Cell 高度缓存

          本文链接:https://www.haomeiwen.com/subject/plchzttx.html