Ios UITextView在UITableView中自动展开
我在Ios UITextView在UITableView中自动展开,ios,uitableview,uitextview,Ios,Uitableview,Uitextview,我在UITableView的最后一行有一个UITextView。我想根据正常工作的文本自动展开UITextView。但是随着UITextView的扩展,UITableViewCell也应该扩展。我在textViewDidChange中尝试了reloadrowsatindexpath,但一次又一次地重新加载单元格,导致UITextView辞职第一响应者 代码如下: - (void)textViewDidChange:(UITextView *)textView { CGRect frame
UITableView
的最后一行有一个UITextView
。我想根据正常工作的文本自动展开UITextView
。但是随着UITextView
的扩展,UITableViewCell
也应该扩展。我在textViewDidChange
中尝试了reloadrowsatindexpath
,但一次又一次地重新加载单元格,导致UITextView
辞职第一响应者
代码如下:
- (void)textViewDidChange:(UITextView *)textView
{
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;
[tableMessageDetail beginUpdates];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(int)txtReplyBottom.tag inSection:0];
[tableMessageDetail reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableMessageDetail endUpdates];
}
Gif
单元自动布局
我添加了>=50的高度约束
代码
和tableviewController
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"SelfSizingCell" bundle:nil] forCellReuseIdentifier:@"cell"];
self.tableView.estimatedRowHeight = 50;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SelfSizingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.tableView = self.tableView;
return cell;
}
为什么要在UITableView的最后一行添加textView?我建议将其设置为TableViewFooter@HossamGhareeb:您建议如何更新TableViewFooter的高度?尝试将页脚框架更新为textView的同一框架如何?我不确定它是否会工作,但是给它一个快照,像这样抓取它
footer=tableView.tableFooterView
,然后更新它的高度。然后再次分配它。tableView.tableFooterView=footer
UITextView不是在IB中创建的。我是以编程方式创建它的。@Nitish,创建一个高度约束,然后更新高度。让heightConstraint=NSLayoutConstraint(项目:newView,属性:NSLayoutAttribute.Height,关联方式:NSLayoutRelation.Equal,到项目:nil,属性:NSLayoutAttribute.NotAttribute,乘数:1,常数:100)查看。添加约束(heightConstraint)
@interface SelfSizingCell : UITableViewCell
@property (weak,nonatomic)UITableView * tableView;
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"SelfSizingCell" bundle:nil] forCellReuseIdentifier:@"cell"];
self.tableView.estimatedRowHeight = 50;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SelfSizingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.tableView = self.tableView;
return cell;
}