不等高cell

作者: Silence_xl | 来源:发表于2020-11-27 13:07 被阅读0次
    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface ListModel : NSObject
    
    @property (nonatomic, copy) NSString *desc;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "ListModel.h"
    
    @implementation ListModel
    
    @end
    
    #import <UIKit/UIKit.h>
    #import "ListModel.h"
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface ListTableViewCell : UITableViewCell
    
    @property (nonatomic, strong) ListModel *model;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "ListTableViewCell.h"
    #import <Masonry/Masonry.h>
    
    @interface ListTableViewCell ()
    
    @property (nonatomic, strong) UIImageView *iconImageView;
    @property (nonatomic, strong) UILabel *label;
    
    @end
    
    @implementation ListTableViewCell
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
            
            [self.contentView addSubview:self.iconImageView];
            [self.contentView addSubview:self.label];
            [self p_addMasonry];
        }
        return self;
    }
    
    #pragma mark - # Private Methods
    - (void)p_addMasonry {
        [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            make.top.mas_equalTo(10);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(80);
        }];
    
        [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.iconImageView.mas_right).mas_offset(10);
            make.top.mas_equalTo(10);
            make.right.mas_equalTo(-10);
            make.bottom.mas_equalTo(-10);
            make.height.mas_greaterThanOrEqualTo(100);
        }];
    }
    
    #pragma mark - # Getter
    - (UIImageView *)iconImageView {
        if (!_iconImageView) {
            _iconImageView = [[UIImageView alloc] init];
            _iconImageView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
        }
        return _iconImageView;
    }
    
    - (UILabel *)label {
        if (!_label) {
            _label = [[UILabel alloc] init];
            _label.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.1];
            _label.numberOfLines = 0;
        }
        return _label;
    }
    
    - (void)setModel:(ListModel *)model {
        _model = model;
        
        self.iconImageView.image = [UIImage imageNamed:@"zrx4.jpg"];
        self.label.text = model.desc;
    }
    
    @end
    
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    @end
    
    #import "ViewController.h"
    #import "ListTableViewCell.h"
    #import <Masonry/Masonry.h>
    
    @interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
    
    @property (nonatomic, strong) NSMutableDictionary *heightAtIndexPath;//缓存高度
    @property (strong, nonatomic) UITableView *tableView;
    @property (nonatomic, strong) NSMutableArray *dataArray;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [self.view addSubview:self.tableView];
        self.navigationItem.title = @"标题";
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"reloaData" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemDidClick:)];
        ///UITableViewCell  的注册
        [self.tableView registerClass:[ListTableViewCell class] forCellReuseIdentifier:NSStringFromClass([ListTableViewCell class])];
    }
    
    ///创建UITableView
    - (UITableView *)tableView {
        if (!_tableView) {
            _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
            _tableView.delegate = self;
            _tableView.dataSource = self;
            _tableView.sectionHeaderHeight = 0.0;
            _tableView.sectionFooterHeight = 0.0;
            _tableView.estimatedSectionHeaderHeight = 0.0;
            _tableView.estimatedSectionFooterHeight = 0.0;
        }
        return _tableView;
    }
    
    #pragma mark - Methods
    - (void)rightBarButtonItemDidClick:(UIBarButtonItem *)item {
        [self.tableView reloadData];
    }
    
    #pragma mark - Getters
    - (NSMutableArray *)dataArray {
        if (!_dataArray){
            _dataArray = [NSMutableArray array];
            NSString *string = @"Siri 让你能够利用语音来完成发送信息、安排会议、查看最新比分等更多事务。只要说出你想做的事,Siri 就能帮你办到。Siri 可以听懂你说的话、知晓你的心意,甚至还能有所回应。iOS 7 中的 Siri 拥有新外观、新声音和新功能。它的界面经过重新设计,以淡入视图浮现于任意屏幕画面的最上层。Siri 回答问题的速度更快,还能查询更多信息源,如维基百科。它可以承担更多任务,如回电话、播放语音邮件、调节屏幕亮度,以及更多。";
            //生成假数据
            for (int i = 0; i < 1000; i++){
                ListModel *model = [[ListModel alloc] init];
                NSInteger index = (arc4random()%(string.length / 20)) * 20;
                model.desc = [string substringToIndex:MAX(20, index)];
                [_dataArray addObject:model];
            }
        }
        return _dataArray;
    }
    
    
    #pragma mark - UITableViewDelegate
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return self.dataArray.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ListTableViewCell class]) forIndexPath:indexPath];
        cell.model = self.dataArray[indexPath.row];
        return cell;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSNumber *height = [self.heightAtIndexPath objectForKey:indexPath];
        if(height){
            return height.floatValue;
        }else{
            return 100;
        }
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return UITableViewAutomaticDimension;
    }
    
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        NSNumber *height = @(cell.frame.size.height);
        [self.heightAtIndexPath setObject:height forKey:indexPath];
    }
    
    #pragma mark - Getters
    - (NSMutableDictionary *)heightAtIndexPath {
        if (!_heightAtIndexPath) {
            _heightAtIndexPath = [NSMutableDictionary dictionary];
        }
        return _heightAtIndexPath;
    }
    
    @end
    
    Simulator Screen Shot - iPhone 11 Pro Max - 2020-11-27 at 13.07.27.png

    相关文章

      网友评论

        本文标题:不等高cell

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