·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> 如何利用autolayout动态计算UITableViewCell的高度

如何利用autolayout动态计算UITableViewCell的高度

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

公司最近要用到autoLayout,今天看了一些autoLayout相关的东西。临下班的时候,一个同事问到如何使用autoLayout实现动态计算UITableViewCell高度,于是一起研究了一番,参考了一篇动态计算UITableViewCell高度详解文章,回家简单实现了使用autoLayout实现了动态计算UITableViewCell高度。


实现上面的步骤其实很简单:

1.在Cell对应的xib文件中建立完整的约束。

2.使用[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingComPRessedSize]方法对cell进行动态计算高度,
然后在方法- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath返回cell的高度。

注意:[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]一定是cell.contentView,不能用cell。


相关代码:

- (void)viewDidLoad{    
    [super viewDidLoad];        
    _data = @[@"数据1", @"数据2",@"数据3", @"数据4", @"数据5", @"数据6"];
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;    
    _tableView.dataSource = self;    
    [self.view addSubview:_tableView];        
    _cell = [[[NSBundle mainBundle] loadNibNamed:@"LWCell" owner:self options:nil] lastObject];    
    _textViewCell = [[[NSBundle mainBundle] loadNibNamed:@"LWTextViewCell" owner:self options:nil] lastObject];          
    _textViewCell.textView.delegate = self;
}
#pragma mark UITableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{    
    return _data.count+5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    if(indexPath.row < _data.count){        
       LWCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PrCell"];
       if(cell == Nil){            
          cell = [[[NSBundle mainBundle] loadNibNamed:@"LWCell" owner:self options:nil] lastObject];   
       }        
    cell.content = _data[indexPath.row];        
    return cell;    
    } else {        
       LWTextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHTextViewCell"]; 
       if(cell == nil){            
          cell = [[[NSBundle mainBundle] loadNibNamed:@"LWTextViewCell" owner:self options:nil] lastObject]; 
          cell.textView.delegate = self;        
       }        
       return cell;    
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{     
    if(indexPath.row < _data.count){        
       _cell.content = _data[indexPath.row];        
       CGSize size = [_cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
      return size.height+1.0f;    
    } else { 
       CGSize size = [_textViewCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 
       _textViewCell.textView.text = _inputText;
       CGSize textViewSize = [_textViewCell.textView sizeThatFits:CGSizeMake(_textViewCell.textView.frame.size.width, 100000.0f)]; 
       return 50 > (size.height + 1.0f + textViewSize.height) ? 50 : (size.height + 1.0f + textViewSize.height);    
    }
}
#pragma mark UITextView delegate- 
(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{    
    if ([text isEqualToString:@"/n"]) {
        NSLog(@"h=%f", textView.contentSize.height);    
    }    
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView
{    
    _inputText = textView.text;
    [_tableView beginUpdates];
    [_tableView endUpdates];
}