• UITableViewCell自定义高度


    第一步 

      在ViewController里,需要有数据,即就是数组,

    第二步  

       创建一个UITableViewCell 

       .h文件中定义两个属性 分别是 一个字符串(用来赋值在TableViewcell 的Label 的Text),一个浮点数记录TableViewcell的高

       .m文件中  定义一个全局变量label  

    //实现这个方法

    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    //重写这个方法

     1 #import <UIKit/UIKit.h>
     2 
     3 @interface CustomTableViewCell : UITableViewCell
     4 /**
     5  *  显示在cell上的字符串
     6  */
     7 @property (nonatomic,copy)NSString * contentStr;
     8 /**
     9  *  cell 高度的最大值
    10  */
    11 @property (nonatomic,assign)CGFloat maxY;
    12 @end
    #import "CustomTableViewCell.h"
    
    @interface CustomTableViewCell ()
    {
        UILabel *_label;
    }
    @end
    
    @implementation CustomTableViewCell
    
    
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
            _label = [[UILabel alloc] init];
            _label.font = [UIFont systemFontOfSize:15];
            _label.numberOfLines = 0;
            // 换行样式
            _label.lineBreakMode = NSLineBreakByCharWrapping;
            [self.contentView addSubview:_label];
        }
        
        return self;
    }
    
    - (void)setContentStr:(NSString *)contentStr
    {
        _contentStr = contentStr;
        // 给label赋值
        _label.text = contentStr;
        CGFloat width = [UIScreen mainScreen].bounds.size.width - 20;
        // 第一个参数:字体的大小 第二个参数:就是文字显示的区域最大值 第三个参数:文字换行的样式
         CGSize size = [_label sizeThatFits:CGSizeMake(width, MAXFLOAT)];
    //    CGSize size = [contentStr sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(width, 999) lineBreakMode:NSLineBreakByCharWrapping];
        _label.frame = CGRectMake(10, 5, width, size.height);
        
        // 获取cell的高度
        _maxY = CGRectGetMaxY(_label.frame) + 10;
    }
    
    
    - (void)awakeFromNib {
        // Initialization code
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
    }

    第三步 展示数据

     1 #import "ViewController.h"
     2 #import "CustomTableViewCell.h"
     3 
     4 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
     5 {
     6     UITableView *_tableView;
     7     NSArray *_array;
     8 }
     9 @end
    10 
    11 @implementation ViewController
    12 
    13 - (void)viewDidLoad {
    14     [super viewDidLoad];
    15     [self getData];
    16     [self addTableView];
    17 }
    18 
    19 - (void)getData
    20 {
    21     _array = @[@"",@"",@"",@""];//此处数据自己填吧
    22 }
    23 
    24 - (void)addTableView
    25 {
    26     _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    27     _tableView.delegate = self;
    28     _tableView.dataSource = self;
    29     [self.view addSubview:_tableView];
    30 }
    31 
    32 
    33 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    34 {
    35     return _array.count;
    36 }
    37 
    38 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    39 {
    40     NSString *ID = @"ID";
    41     CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    42     if (cell == nil) {
    43         cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    44     }
    45     
    46     cell.contentStr = _array[indexPath.row];
    47     // 设置cell的高度
    48     tableView.rowHeight = cell.maxY;
    49     return cell; 
    50 }

    需要注意的是上面代码第48行,他在这里为TableViewcell 设置了Height

    运行代码之后的结果是

  • 相关阅读:
    进程池-非阻塞式
    进程
    单例模式
    Python内置函数之open()
    Python内置函数之len()
    Python内置函数之isinstance()
    Python内置函数之input()
    可迭代对象,迭代器(生成器)区别
    Tomcat控制台输出
    Python内置函数之format()
  • 原文地址:https://www.cnblogs.com/fanwenzheIOS/p/4978825.html
Copyright © 2020-2023  润新知