The only problem I came across with the accepted answer was that we distribute a UITextView every time. I found that this caused problems with entering the view and updating the text immediately, as well as saving the view as the first responder. When I tried to reload a cell with a new height, it would try to add a new text element.
Because of this, I found a slightly different method to achieve the same goal. Hope this other solution can help people trying to implement the above code.
1) In the header file, specify a variable for the height of the text and textView:
UITextView * _textView; NSInteger _textHeight;
Setting a variable means that we can load the view as a specific height if we load text into a textView and also reduce complexity.
2) Download the text view and add it to our cell
_textView = [[UITextView alloc] init]; _textView.delegate = self; _textView.returnKeyType = UIReturnKeyDone; _textView.font = [UIFont fontWithName:@"Helvetica" size:16]; if (![_user metaStringForKey:bBioKey].length) { _textView.text = @"Placeholder text"; _textView.textColor = [UIColor lightGrayColor]; } - (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath == 0) {
Using keeplayout allowed us to keep our text field always at the same height as the cell. We also add only UITextView once.
3) Add code to calculate text height
- (NSInteger)getHeightOfBio: (NSString *)text { UILabel * gettingSizeLabel = [[UILabel alloc] init]; gettingSizeLabel.font = [UIFont fontWithName:@"Helvetica" size:16]; gettingSizeLabel.text = text; gettingSizeLabel.numberOfLines = 0; CGSize maximumLabelSize = CGSizeMake(240, 9999);
I tried a lot and found that it works better
4) Add some logic to the cell height to use this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) {
Obviously, we need a little more height than the height of the text, so I added an extra pillow size that you can experiment with.
5) Refresh the view every time a character is typed to check if you need to increase the size:
- (void)textViewDidChange:(UITextView *)textView { if ([self getHeightOfText:textView.text] != _textHeight) { _textHeight = [self getHeightOfText:textView.text]; [self.tableView beginUpdates]; [self.tableView endUpdates]; } }
In this section, we get the height of the text every time the user types.
Then we use our stored value and compare the current value with the stored value. Obviously, if they are the same, it makes no sense to update the view. If they differ, we update the value and then update our table.
This bit I found a nice answer to stackOverflow showing how we can only update table heights instead of the cell itself. Why update the cell when we donβt need it? This means that as soon as it is called, the cell height is updated and it increases well.
In any case, I found that it worked very nicely and was simple enough so that it could be put together or put together different parts and put into other pieces of code.
Confirms the accepted answer, which was plundered for various objects along the way, but I also hope that this answer will help some people who are experiencing the same difficulties as me.