1
votes

I have a static UITableViewCell that contains a UITextView (that dynamically gets resized based on the content it has,) and a UIView.

The cell's height dynamically changes based on the height of the textView and the view.

Here is my code:

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

    return YES;
}

- (void)textViewFitToContent:(UITextView *)textView
{
    textView.scrollEnabled = YES;
    CGFloat fixedWidth = textView.frame.size.width;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = textView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    textView.frame = newFrame;
}

- (void)scrollToLocation: (UITextView *)textView
{
    UIView *parentView = textView.superview;
    while (![parentView isKindOfClass:[UITableViewCell class]]) {
        parentView = parentView.superview;
    }
    NSIndexPath *indexPath = [myTableView indexPathForCell:(UITableViewCell *)parentView];
    [myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int padding = 48;

    if (indexPath.section == 0 && indexPath.row == 0)
    {
        return 163;
    }
    else if (indexPath.section == 1 && indexPath.row == 0) {
        return textView1.frame.size.height + self.view1.frame.size.height + padding;
    }
    else if (indexPath.section == 1 && indexPath.row == 1) {
       return textView2.frame.size.height + self.view2.frame.size.height + padding;
    }
    else if (indexPath.section == 1 && indexPath.row == 2) {
        return textView3.frame.size.height + self.view3.frame.size.height + padding;
    }
    else if (indexPath.section == 1 && indexPath.row == 3){
        return textView4.frame.size.height + self.view4.frame.size.height + padding;
    }

    return 44;
}

Problem

The problem is, when I type in the first textView (it happens to every letter I type,) all other textView's height becomes smaller. If I type in any other textViews, (not the first one,) the first textViews height becomes bigger, and all other textViews height becomes smaller.

1

1 Answers

0
votes

Give each textView a tag. If they're in TableViewCells...make the indexPath.row the tag for each textView. In your UITextViewDelegate methods. Use an IF ELSE and do something like

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (textView.tag == 1) {
        [self textViewFitToContent:textView];
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
        [self scrollToLocation:textView];
    }
    if (textView.tag == 2) {
        [self textViewFitToContent:textView];
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
        [self scrollToLocation:textView];
    }
    if (textView.tag == 3) {
        [self textViewFitToContent:textView];
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
        [self scrollToLocation:textView];
    }

    return YES;
}