iOS点点滴滴ios开发iOS 开发每天分享优质文章

UITextView 自动行高 在 UITableView 中

2018-07-17  本文已影响14人  新南12138

简介

开发中会大量的使用 UITableView,当我们有很多的cell,每一行都有一个 UITextView 用来进行稍微重一些的文本编辑,这个时候,你的产品经理突然给你说,我希望这个 UITextView会随着书写内容的长度而自动的增高。(꒪ꇴ꒪(꒪ꇴ꒪ ;)哈?

为什么要写这个

因为我自己也找了好久,参照过别人写的一些,但是到我这里都不是特别的实用,尤其是进行数据刷新的时候,如果你的行高需要改变,你去 reloadData这个时候,键盘会失去响应。就算你这个时候再次将键盘设置为 firstResponser,这个时候你的键盘还是会闪一下,不是很自然。所以写了一些小的 demo ,下面的具体代码也是针对这个 demo 的,点击这里下载

思路

  1. 首先对 TextView 的输入进行监听,判断输入的字数的多少
  2. 根据 UITextView 的字数的多少去动态的修改 UITextView 的 frame
  3. 监听 UITextView 的高度变化,如果 UITextView 比你的 cell 还要高的时候,你需要动态的去更新你的 UITableViewCell 的高度

实现

1.对 UITextView 的输入进行监听,判断输入的字数的多少

前人已经有了现成的轮子,所以不需要你重复的造轮子。这里找到一个很好的自动换行的 TextView我们需要使用这个来完成我们接下来的工作

   [textView wzb_autoHeightWithMaxHeight:300 textViewHeightDidChanged:^(CGFloat currentTextViewHeight) {
     //在此处我们已经对 UITextView 完成了监听,接下来我们就可以修改 UITextView 的 frame 了
}];

2.根据 UITextView 的字数的多少去动态的修改 UITextView 的 frame

__weak typeof (self)WeakSelf = self;
__weak typeof (textView)WeakTextView = textView;    
[textView wzb_autoHeightWithMaxHeight:300 textViewHeightDidChanged:^(CGFloat currentTextViewHeight) {
    CGRect frame = WeakTextView.frame;
    frame.size.height = currentTextViewHeight;
    [UIView animateWithDuration:0.2 animations:^{
        WeakTextView.frame = frame;
        
    } completion:^(BOOL finished) {
    }];

 }];

3.动态的修改cell 的高度

我们需要对单元格的高度进行保存,然后在刷新行高的时候把高度取出来赋值。所以我们还需要一个保存高度的字典dicHeight

 __weak typeof (self)WeakSelf = self;
__weak typeof (textView)WeakTextView = textView;
// 最大高度为300 改变高度的 block
[textView wzb_autoHeightWithMaxHeight:300 textViewHeightDidChanged:^(CGFloat currentTextViewHeight) {
    CGRect frame = WeakTextView.frame;
    frame.size.height = currentTextViewHeight;
    [UIView animateWithDuration:0.2 animations:^{
        WeakTextView.frame = frame;
        
    } completion:^(BOOL finished) {
    }];
    
    NSString *key = [NSString stringWithFormat:@"%@",indexPath];
    NSNumber *height = [NSNumber numberWithFloat:currentTextViewHeight];
    if (WeakSelf.dicHeight[key]) {
        NSNumber *oldHeight = self.dicHeight[key];
        if (oldHeight.floatValue != height.floatValue) {
            [WeakSelf.dicHeight setObject:height forKey:key];
        }
    }
    else{
        [WeakSelf.dicHeight setObject:height forKey:key];
        
    }
    
    [WeakSelf.tableview beginUpdates];
    [WeakSelf.tableview endUpdates];
    
}];

我们使用beginUpdatesendUpdates 。不使用 reloadData。具体的原因看我之前写的为什么不用 ReloadData

接下来就可以修改行高了

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *key = [NSString stringWithFormat:@"%@",indexPath];
    if(self.dicHeight[key]){
      NSNumber *number = self.dicHeight[key];
      if (number.floatValue > 44) {
          return number.floatValue;
          }
      }
      return 44;
  }

小彩蛋

通过这个你就可以简单的知道为什么不能用 reloadData了。使用的时候把自动行高的哪里注释掉。简单的输入一些东西做一些尝试吧。

  //模拟数据刷新
-(void)textViewDidChange:(UITextView *)textView{

//1.reloadData
//[self.tableview reloadData];

 //2.reloadindexpath
 //    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
 //    [self.tableview reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationTop];


//3.beginupdate & endupdate
//    [self.tableview beginUpdates];
//    [self.tableview endUpdates];
  
}

总结

UITableView 作为 iOS 开发中用到的比较多的控件,在具体的使用中总会出现各种各样的问题,自动行高这里总算是脱坑了。先写到这里

上一篇下一篇

猜你喜欢

热点阅读