Change UITextView height in cellForRowAtIndexPath

I am trying to change the height of a UITextView that I have in a custom made cell for my tableView.

"Browse Cell" is the identifier of this custom cell at the tip, and the UITextView "postIntro" is dragged into nib.

Unfortunately, the height does not change, only after I scroll the height has changed. Cells that are visible without scrolling are only the default height from nib.

When I try to change the height of "postImage", the same problem occurs.

This is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"OverviewCell"; OverviewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; Post *post = [self.posts objectAtIndex:[indexPath row]]; if(!cell) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"OverviewCell" owner:nil options:nil]; for(id currentObject in topLevelObjects) { if([currentObject isKindOfClass:[OverviewCell class]]) { cell = (OverviewCell *)currentObject; break; } } } [[cell postImage] setImage:[UIImage imageWithData:post.image]]; [[cell postTitle] setText:post.title]; [[cell postIntro] setText:post.intro]; // Resize the intro textview so the text fits in. CGRect frame = cell.postImage.frame; frame.size.height = 20; //cell.postIntro.contentSize.height; [cell.postIntro setFrame:frame]; NSLog([NSString stringWithFormat:@"Height of textView on indexpath %i is set to %f", indexPath.row ,cell.postImage.frame.size.height]); return cell; } 

Any other suggestions for my code are welcome ...

+4
source share
3 answers

I finally started working by changing the UITextview restriction in the code.

I also created an IBOutlet that is bound to a UITextview constraint.

 IBOutlet NSLayoutConstraint * postContentHeightConstraint; 

After that, I set the restriction height for the specified height for the cell ( contentSize ).

 self.postContentHeightConstraint.constant = self.postContent.contentSize.height; 

After setting the new height to the constraint, call the layoutIfNeeded function in a UITextView

 [self.postContent layoutIfNeeded]; 
+2
source

Add tableView: heightForRowAtIndexPath: to the table view delegate.

Refresh . To update the height of a UITextView after creating it, call [cell.postIntro setNeedsLayout] after setting the frame

0
source

To change the height of a UITextView , call

 [tableView beginUpdates]; [tableView endUpdates]; 

after setting new parameters for UITextView.

This questions, and that will help with your problem.

Hope the answer helped!

0
source

Source: https://habr.com/ru/post/1487459/