动态调整UITextView高度

时间:2015-01-28 02:09:13

标签: objective-c uitableview uitextview

我有一个静态UITableViewCell,它包含一个UITextView(根据它拥有的内容动态调整大小)和UIView。

单元格的高度根据textView和视图的高度动态变化。

这是我的代码:

- (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;
}

问题

问题是,当我输入第一个textView(它发生在我键入的每个字母时),所有其他textView的高度变小。如果我输入任何其他textViews(不是第一个),第一个textViews高度变大,所有其他textViews高度变小。

1 个答案:

答案 0 :(得分:0)

为每个textView提供一个标记。如果他们在TableViewCells中...使indexPath.row成为每个textView的标记。在您的UITextViewDelegate方法中。使用IF ELSE并执行类似

的操作
- (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;
}
相关问题