在键入时调整包含UITextView的UITableViewCell [英] Resize UITableViewCell containing UITextView upon typing

查看:122
本文介绍了在键入时调整包含UITextView的UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含自定义单元格的UITableViewController。每个单元格都是使用nib创建的,并包含一个不可滚动的UITextView。我在每个单元格中添加了约束,以便单元格将其高度调整为UITextView的内容。所以最初我的控制器看起来像这样:





现在我想要当用户在单元格中键入内容时,其内容会自动适应。这个问题已被多次询问,特别是



如果我向下滚动tableView为了强制重新调用cellForRowAtIndexPath,我恢复了单元格的正确高度:





请注意,我实现了heightForRowAtIndexPath,因为我希望autoLayout能够处理这个问题。



有人能告诉我我做错了什么或帮助我吗?非常感谢!

解决方案

受到前两个答案的启发,我找到了解决问题的方法。我认为我有一个UITextView的事实导致了autoLayout的一些麻烦。我在原始代码中添加了以下两个函数。

   - (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat )width {
UITextView * calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:text];
CGSize size = [calculationView sizeThatFits:CGSizeMake(width,FLT_MAX)];
返回size.height;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UIFont * font = [UIFont systemFontOfSize:14.0] ;
NSDictionary * attrsDictionary = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString * attrString = [[NSAttributedString alloc] initWithString:self.sampleStrings [indexPath.row] attributes:attrsDictionary];
return [self textViewHeightForAttributedText:attrString andWidth:CGRectGetWidth(self.tableView.bounds)-31] +20;
}

最后一行的位置 31 是我对单元格左侧和右侧的约束的总和, 20 只是一些随意的松弛。



我在阅读这个非常有趣的答案时找到了这个解决方案


I have an UITableViewController that contains a custom cell. Each cell was created using a nib and contains a single non-scrollable UITextView. I have added constraints inside each cell so that the cell adapts its height to the content of the UITextView. So initially my controller looks like this :

Now I want that when the user types something in a cell its content automatically adapts. This question has been asked many times, see in particular this or the second answer here. I have thus written the following delegate in my code :

- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
    return YES;
}

However it leads to the following strange behavior : all constraints are ignored and all cells height collapse to the minimal value. See the picture below:

If I scroll down and up the tableView in order to force for a new call of cellForRowAtIndexPath, I recover the correct heights for the cells:

Note that I did not implement heightForRowAtIndexPath as I expect autoLayout to take care of this.

Could someone tell me what I did wrong or help me out here ? Thank you very much !

解决方案

Inspired by the two previous answers, I found a way to solve my problem. I think the fact that I had a UITextView was causing some troubles with autoLayout. I added the following two functions to my original code.

- (CGFloat)textViewHeightForAttributedText: (NSAttributedString*)text andWidth: (CGFloat)width {
    UITextView *calculationView = [[UITextView alloc] init];
    [calculationView setAttributedText:text];
    CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)];
    return size.height;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UIFont *font = [UIFont systemFontOfSize:14.0];
    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.sampleStrings[indexPath.row] attributes:attrsDictionary];
    return [self textViewHeightForAttributedText:attrString andWidth:CGRectGetWidth(self.tableView.bounds)-31]+20;
}

where in the last line 31 is the sum of my constraints to the left and right sides of the cell and 20 is just some arbitrary slack.

I found this solution while reading this this very interesting answer.

这篇关于在键入时调整包含UITextView的UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆