iOS UITextView 输入内容实时更新cell的高度
iOS UITextView 输入内容实时更新cell的高度
这篇文章介绍了在一个动态数据的 table view 中,cell 根据 text view 内容的输入实时改变 cell 和 table view 的高度。自动计算 cell 高度的功能使用 iOS 8 才支持的自适应 cell,如果你还不知道 iOS 8 自适应 cell,可以参看这篇文章:iOS 8 自适应 Cell
先上图,我们最终要实现的效果是这样的:

图 1:实时更新 cell 高度
实现上面效果的基本原理是:
在 cell 中设置好 text view 的 autolayout,让 cell 可以根据内容自适应大小
text view 中输入内容,根据内容更新 textView 的高度
调用 tableView 的 beginUpdates 和 endUpdates,重新计算 cell 的高度
将 text view 更新后的数据保存,以免 table view 滚动超过一屏再滚回来 text view 中的数据又不刷新成原来的数据了。
功能具体实现方法
新建一个项目,拉出 TableViewController,在 cell 上添加一个 UITextView。
首先设置 text view 的 autolayout,比较关键的 constraint 是要设置 textView 的高度大于等于一个值。如图:

图 2: Text view 的 autolayout 设置
然后,设置 UITextView 的 scrollEnable 为 NO。这一点很关键,如果不设置为 NO,UITextView 在内容超出 frame 后,重新设置 text view 的高度会失效,并出现滚动条。

图 3:去掉 scrolling enable 勾选
根据刚才在 storyboard 中创建的 cell,新建一个 UITableViewCell 类。
|
1
2
3
4
|
#import <uikit uikit.h="">@interface TextViewCell : UITableViewCell@property (weak, nonatomic) IBOutlet UITextView *textView;@end</uikit> |
创建 TableViewController 并初始化一些数据
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#import "TableViewController.h"#import "TextViewCell.h"@interface TableViewController ()@property (nonatomic, strong) NSArray *data;@end@implementation TableViewController- (void)viewDidLoad { [super viewDidLoad]; // 支持自适应 cell self.tableView.estimatedRowHeight = 100; self.tableView.rowHeight = UITableViewAutomaticDimension; self.data = @[@"Cell 1 ", @"Cell 2", @"Cell 3", @"Cell 4", @"Cell 5", @"Cell 6", @"Cell 7", @"Cell 8"];}#pragma mark - Table view data source- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.data count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextViewCell" forIndexPath:indexPath]; cell.textView.text = self.data[indexPath.row]; return cell;} |
使用上面的代码项目已经可以运行了,但是 text view 还不能自动更新大小,下面来实现 text view 根据内容计算高度。
先在 storyboard 中,将 UITextView 的 delegate 设置为 cell

图 4:设置 UITextView 的 delegate 为 cell
在 TextViewCell.m 中实现 - (void)textViewDidChange:(UITextView *)textView,每次 text view 内容改变的时候,就重新计算一次 text view 的大小,并让 table view 更新高度。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#import "TextViewCell.h"@implementation TextViewCell- (void)textViewDidChange:(UITextView *)textView{ CGRect bounds = textView.bounds; // 计算 text view 的高度 CGSize maxSize = CGSizeMake(bounds.size.width, CGFLOAT_MAX); CGSize newSize = [textView sizeThatFits:maxSize]; bounds.size = newSize; textView.bounds = bounds; // 让 table view 重新计算高度 UITableView *tableView = [self tableView]; [tableView beginUpdates]; [tableView endUpdates];}- (UITableView *)tableView{ UIView *tableView = self.superview; while (![tableView isKindOfClass:[UITableView class]] && tableView) { tableView = tableView.superview; } return (UITableView *)tableView;}@end |
这样就已经实现了 text view 改变内容自动更新 cell 高度的功能,这篇文章没有涉及到计算 cell 高度的代码,因为计算 cell 高度的工作全部交给 iOS 8 的 autolayout 自动计算了,这让我们少写了许多令人痛苦的代码。
最后:为了防止 table view 过长,导致滚动后重新加载 cell,会让 text view 中的内容还原的问题,我们应该在更新了 text view 的内容之后保存数据。(如果是在编辑状态下,还需要考虑取消编辑后的回滚功能。 普通数组数据,可以保存一个原始数据的副本,如果用户取消编辑,就设置 data 为原始数据的副本。如果是 NSManagedObject 对象可以使用 NSUndoManage,不过这些已经超出本篇文章的内容范围了。)
为了在 text view 更新后能让 TableViewController 中的 data 更新,需要为 cell 添加一个 delegate,在 text view 更新后调用 delegate,TableViewController 中收到 delegate 信息后更新 data。
修改后的 TextViewCell.h
|
1
2
3
4
5
6
7
8
9
|
#import <uikit uikit.h="">@protocol TextViewCellDelegate;@interface TextViewCell : UITableViewCell@property (weak, nonatomic) IBOutlet UITextView *textView;@property (weak, nonatomic) id<textviewcelldelegate> delegate;@end@protocol TextViewCellDelegate <nsobject>- (void)textViewCell:(TextViewCell *)cell didChangeText:(NSString *)text;@end</nsobject></textviewcelldelegate></uikit> |
在 TextView.m的 - (void)textViewDidChange:(UITextView *)textView 中添加 delegate 的调用
|
1
2
3
4
5
6
7
8
9
10
|
- (void)textViewDidChange:(UITextView *)textView{ if ([self.delegate respondsToSelector:@selector(textViewCell:didChangeText:)]) { [self.delegate textViewCell:self didChangeText:textView.text]; } // 计算 text view 的高度 ... // 让 table view 重新计算高度 ...} |
最后在 TableViewController.m 的最后实现 TextViewCellDelegate 的方法,更新 data
|
1
2
3
4
5
6
7
8
|
#pragma mark - TextViewCellDelegate- (void)textViewCell:(TextViewCell *)cell didChangeText:(NSString *)text{ NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; NSMutableArray *data = [self.data mutableCopy]; data[indexPath.row] = text; self.data = [data copy];} |
总结
从最后的代码量来看,这个功能在实现上并不难。只要记住的几点:
使用 iOS 8 的特性自动计算 cell 高度,或者在 heightForRow 中自己实现计算高度的代码。
UITextView 的 scrollEnable 要设置 NO
更新 table view 的高度使用 beginUpdates 和 endUpdates
Text view 更新内容后要保存数据,以免重新加载 cell 时数据丢失
参考
https://pontifex.azurewebsites.net/self-sizing-uitableviewcell-with-uitextview-in-ios-8/
http://stackoverflow.com/questions/50467/how-do-i-size-a-uitextview-to-its-content/26599389#26599389
http://stackoverflow.com/questions/18368567/uitableviewcell-with-uitextview-height-in-ios-7
iOS UITextView 输入内容实时更新cell的高度的更多相关文章
- MFC编辑框接收数据动态更新与刷新方法代码示例-如何让编辑框内容实时更新
MFC编辑框接收数据动态更新与刷新方法代码示例-如何让编辑框内容实时更新 关键代码: //发送数据通知 //from txwtech@163.com LRESULT CCommSampleDlg::O ...
- 【C#】让DataGridView输入中实时更新数据源中的计算列
本文适用Winform开发,且DataGridView的数据源为DataTable/DataView的情况. 理解前提:熟知DataTable.DataView 求:更好方案 考虑这样一个场景: 某D ...
- iOS 依据文本内容为TextView动态定义高度
解决方式: 1.定义一个textview,在storyboard中设定该textview的constraints. 2.将高度的constraint定义到头文件里:(直接拖拽) @property ( ...
- iOS之UITableView加载网络图片cell自适应高度
#pragma mark- UITableView - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSI ...
- IOS UITextView支持输入、复制、粘贴、剪切自定义表情
UITextView是ios的富文本编辑控件,除了文字还可以插入图片等.今天主要介绍一下UITextView对自定义表情的处理. 1.首先识别出文本中的表情文本,然后在对应的位置插入NSTextAtt ...
- [iOS] 输入框高度随输入内容变化
一般,类似聊天软件的输入框默认都是显示一行的,在用户输入过程中根据输入文字的内容来改变输入框的高度,以便显示全部文字.像微信,QQ的输入框就是这样的.那么这个效果应该怎么实现呢? 新博客:wosson ...
- iOS开发-UITextView根据内容自适应高度
UITextView作为内容文本输入区域,有的时候我们需要根据内容动态改变文本区域的高度,效果如下: 定义UITextView,实现UITextViewDelegate: -(UITextView * ...
- iOS学习网站及大牛网址(实时更新)
iOS学习网站及大牛网址(实时更新) 学习网站 https://github.com/Tim9Liu9/TimLiu-iOS 自己总结的iOS.mac开源项目及库 https://github.co ...
- iOS回顾笔记(09) -- Cell的添加、删除、更新、批量操作
iOS回顾笔记(09) -- Cell的添加.删除.更新.批量操作 项目中经常有对UITableViewCell做各种操作的需求: 添加一个新的cell 删除某行cell 刷新cell上某行数据(如修 ...
随机推荐
- RPM卸载
安全地卸载 rpm卸载软件包,并不是简单地将原来安装的文件逐个删除,那样做的话,可能会出现这样或那样的问题.如,a软件包依靠b软件包做某些工作,若b软件包卸载了,则a软件包就不能正常运行了.rpm为用 ...
- Tomcat、Websphere和Jboss类加载机制
http://blog.csdn.net/lshxy320/article/details/6448972 2 Tomcat 类加载机制 Tomcat Server 在启动的时候将构造一个 ...
- Redis Key过期通知
概述 键空间通知使得客户端可以通过订阅频道或模式, 来接收那些以某种方式改动了 Redis 数据集的事件.如Redis数据库中键的过期事件也是通过订阅功能实现.本文主要基于Azure PaaS Red ...
- 将ViewState放在Session里(转载)
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Ent ...
- Mysql暴错注入代码-webshell
MySql Error Based Injection Reference[Mysql暴错注入参考]Author:Pnig0s1992Mysql5.0.91下测试通过,对于5+的绝大部分版本可以测 ...
- linux下的ssh与ssh客户端
经常会看到ssh客户端,或者听到ssh到某台机器..问题:ssh和ssh客户端什么关系? 1.ssh,secure shell,是一种网络交互协议,也指实现该协议的网络服务程序.主要用于远程机器管理, ...
- Tokyo Tyrant(TTServer)系列(六)-数据丢失谁的错
,false,1,100);$mem->addServer ("127.9.9.1",1978,false,1,100);$start=microtime(true);for ...
- php-fpm配置详解
php-fpm详解 原文链接:http://php-fpm.anight.org/wiki:http://www.php-fpm.com/翻译:http://syre.blogbus.com/logs ...
- nutch solr
创建solr数据目录 创建目录solrData,拷贝solr-4.10.2/example/solr到solrData下 修改配置文件中数据目录路径 修改 solrData/solr/coll ...
- ECMall 中URL体系的改造思路
EC系列的产品都已停止更新很久了,但其对中国中小电商企业的影响无疑是巨大的.很多公司,都是直接拿来即改,改了即用. 但他们都有个问题,代码是比较传统的开发模式过来的,尤其ecshop.ECMall系统 ...