不等高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

    一、自定义Cell1、等高cell 代码 很古老的方法: 利用autoLayout xib加载xib要通过手动加载...

  • 不等高cell自定义的思路精髓

    ## 精华界面不等高cell的搭建 1 . 精华界面搭建,tableview展示数据; 不等高cell(复杂界面)...

  • 基础 (十六) : 等高cell

    等高Cell-frame: 等高Cell-frame 知识回顾 拖入plist文件以及图片 1.(tableVie...

  • 自定义不等高cell

    自定义不等高cell 自定义不等高cell(纯代码) 给模型增加frame数据 所有子控件的frame cell...

  • 巧用AutoLayout自动计算cell高度

    cell的高度 cell的高度可以简单分为等高和不等高两大类。等高的情况不用多说,直接设置表视图的cellHeig...

  • cell的等高与不等高

    自定义等高的cell 等高的cell 所有cell的高度是一样的 纯代码创建 frame 1,新建一个继承自UIT...

  • 自定义等高的cell

    自定义等高的cell 等高的cell 所有cell的高度是一样的 纯代码创建 frame 1,新建一个继承自UIT...

  • day10-UITableView-自定不等高的cell-sto

    自定不等高的cell-storyboard版 01-无配图自定不等高的cell-storyboard版 02-有配...

  • 不等高Cell

    计算字符串一行的高度 计算字符串多行的高度

  • 不等高cell

    纯代码创建 frame 一.给模型增加frame数据 所有子控件的frame cell的高度 二.重写模型cell...

网友评论

    本文标题:不等高cell

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