zoukankan      html  css  js  c++  java
  • 如何利用autolayout动态计算UITableViewCell的高度

    公司最近要用到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];
    }
    
  • 相关阅读:
    [Android] UI疑问?
    [Android] Gradle sync failed: Unsupported method: BaseConfig.getApplicationIdSuffix().
    [Android] 使用GSON解析json成Bean
    2016工作总结
    【UE】关于UE的一个真实案例
    我在新蛋系的这八年
    关于在线预览word,excel,ppt,pdf的需求处理方法。
    【点滴积累,厚积薄发】windows schedule task中.exe程序的路径问题等问题总结
    【点滴积累,厚积薄发】windows schedule task的最小时间间隔是多少?
    【点滴积累,厚积薄发】修改hosts,并刷新dns缓存
  • 原文地址:https://www.cnblogs.com/lichwu/p/4346783.html
Copyright © 2011-2022 走看看