【问题标题】:Dynamically resize UITextView height动态调整 UITextView 高度
【发布时间】:2015-01-28 02:09:13
【问题描述】:

我有一个静态 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 高度变小。

【问题讨论】:

    标签: objective-c uitableview uitextview


    【解决方案1】:

    给每个 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;
    }
    

    【讨论】:

    • 这有什么帮助?
    • @MikeRally 你的委托方法......当你对“textView”做一些事情时,你就是在对所有的 textViews 做一些事情。如果您指定您想要的 textView(通过标签),您将一次更改一个 textView。
    • 我只是为每个 textView 做了 if (textView == textView1) {...},同样的事情发生了
    • 你是用什么方法放的?
    • textView shouldChangeTextInRange