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

  • 相关阅读:
    高斯过程回归
    第一行代码读书笔记3+错误分析
    多项式各种操作
    [BZOJ3625] [Codeforces Round #250]小朋友和二叉树
    [BZOJ2055] 80人环游世世界
    [BZOJ3698] XWW的难题
    [BZOJ3456] 城市规划
    分治FFT
    [BZOJ5306] [HAOI2018]染色
    [BZOJ3380] [USACO2004 Open]Cave Cows 1 洞穴里的牛之一
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4517365.html
Copyright © 2011-2022 走看看