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

  • 相关阅读:
    Jedis测试redis
    jedis池的作用
    错误
    Ceph剖析:数据分布之CRUSH算法与一致性Hash
    drools规则引擎初探
    Techniques for HA IT Management
    django_simple_captcha使用笔记
    微服务架构的理论基础
    分布式系统服务的稳定性
    四层、七层负载均衡的区别
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4517365.html
Copyright © 2011-2022 走看看