zoukankan      html  css  js  c++  java
  • ios uitableviewcell动态计算高度

    #import <UIKit/UIKit.h>
    
    @interface TestCell : UITableViewCell
    @property (weak, nonatomic) IBOutlet UILabel *testLabel;
    - (void)parseResult:(NSString *)result;
    @end
    #import "TestCell.h"
    
    @implementation TestCell
    
    - (void)awakeFromNib {
      
      
        
    }
    - (void)parseResult:(NSString *)result
    {
        _testLabel.text= result;
    }
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
    }
    
    @end
    #import "ViewController.h"
    #import "TestCell.h"
    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
    {
        
        NSArray *dataArray;
    }
    @property (weak, nonatomic) IBOutlet UITableView *testTableView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        dataArray = @[@"1234
    56789012
    34567
    ]
    890
    12341
    2
    3
    4
    5
    6",  @"12345
    67890
    1234
    56789
    01234
    567890", @"1
    2", @"1
    2
    3", @"1
    "];
        
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return dataArray.count;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TestCell" forIndexPath:indexPath];
        [cell parseResult:dataArray[indexPath.row]];
        return cell;
    
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TestCell"];
        [cell parseResult:dataArray[indexPath.row]];
        CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        NSLog(@"第%ld行的高度=%f",indexPath.row ,size.height + 1);
        return 1  + size.height;
    
        
    }
    
    
    @end

  • 相关阅读:
    自定义input标签输入框
    sys.argv 启动时可以传入变量
    falcon 监控
    wrk 压测工具
    mysql UPDATE和REPLACE
    tesseract识别图片中文字
    centos 查看日志 & 查找文件、目录、内容 & centos查看磁盘使用情况
    压力测试
    tensorflow + scikit-learn
    Pycharm快捷键配置
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4517365.html
Copyright © 2011-2022 走看看