UITextView(在UITableViewCell中)跳转到顶部而不是滚动到位置 [英] UITextView (in UITableViewCell) jumps to top instead of scrolling to location

查看:72
本文介绍了UITextView(在UITableViewCell中)跳转到顶部而不是滚动到位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义UITableView中有一个UITextView。我正在尝试使高度自动调整大小,并滚动到正确的位置(光标位置)。



I have a UITextView in a custom UITableView. I'm trying to make the height dynamically size itself, and scroll to the correct location (cursors location).

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        NSRange cursor = NSMakeRange(range.location + 1, 0);

        NSMutableString *mutableT = [NSMutableString stringWithString:textView.text];
        [mutableT insertString:@"\n " atIndex:range.location];
        [textView setText:mutableT];

        [textView setSelectedRange:cursor];
        [textView sizeToFit];
        [textView layoutIfNeeded];

        [self.myTableView beginUpdates];
        [self.myTableView endUpdates];
        return NO;
    }
    return YES;
}





首先:,当文本必须转到下一行时,用户不输入新行(单词换行),然后整个代码不起作用。



第二:当用户输入一个新行,并且它不在视图中时,tableView会滚动到顶部。然后,当我输入另一个char时,它恢复正常。



First:, when the text has to go to the next line, without the user enter a new line (word wrap), then this whole code doesn't work.

Second: When the user enters a new line, and it gets out of view, the tableView gets scrolled to the top. Then when I enter another char, it gets back to normal.

推荐答案

我将禁用textview上的自动滚动。



此代码可以完成这项工作:



I would disable autoscrolling on the textview.

This code should do the job:

[textView scrollRangeToVisible:[textView selectedRange]];


这篇关于UITextView(在UITableViewCell中)跳转到顶部而不是滚动到位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆