ios - 替换UITextView中的文本运行缓慢

标签 ios objective-c uitextview

我有一个UITextView,并且在UITextView.text的末尾替换了一个单词,如以下代码所示:

-(void)replaceTextViewWithWord:(NSString *)word {
    NSString *currentTextViewText = self.textView.text;

    currentTextViewText = [currentTextViewText stringByReplacingCharactersInRange:NSMakeRange([self.textView.text length] - [word length], [word length]) withString:word];

    self.textView.text = currentTextViewText;    
}

替换工作正常,但是当textView变长时,它运行得非常缓慢。

还有其他更好的方法吗?

我尝试使用 replaceRange:withText 一周,但仍然卡住。
在我的情况下,不知道如何使用它来替换。

请指导!提前致谢!这是一项紧迫的任务。

最佳答案

当您使用不可变字符串时,您将创建一个非常长的字符串并将其分配给已创建的NSString。

代替NSString使用NSMutableString可以减少一些开销。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FooBarCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if (cell == nil) {
        cell = [[EditCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier];
        cell.mutableString = [NSMutableString string];
        cell.textView.text = cell.mutableString;
    }
    [cell.mutableString setString:@"text to show"];
    return cell;
}

关于ios - 替换UITextView中的文本运行缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15539269/

相关文章:

ios - 如何在键盘覆盖时移动 UITextView

ios - 如何使我的键盘弹出窗口更加流畅,我的 View Controller 以模态方式出现?

objective-c - 读取符号文件

ios - NSInvalidArgumentException 原因 : data parameter is nil in UITableView while trying to display the Flickr images

ios - 在 TableViewController 中获取 iAd 横幅时遇到问题

swift - TextView自动输入回车键【Mac Catalyst】

ios - 无论设备方向如何,物体都会落向地球

ios - 找出 Objective-C 中异常的原因

iphone - 如何在 Objective-C 中进行十进制格式化?

ios - 如何打印NSSet的内容?