本文介绍了UITextView 中的替换文本运行缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITextView,我替换了 UITextView.text 末尾的一个词,如下代码:

I have an UITextView and I replace a word at the end of UITextView.text like below code:

-(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 变长时,它运行得非常缓慢而且非常缓慢.

Replacement work OK, but it run so slowly and very slowly when textView become longer.

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

I try with replaceRange:withText for a week but still stuck. Don't know how to use it to replace in my case.

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

Please guide! Thanks in advance! It's a urgent task.

推荐答案

由于您使用的是不可变字符串,并且您正在创建一个非常长的字符串并将其分配给已创建的 NSString.

As you are using Immutable string and you are creating a very very long string and assigning it to already created NSString.

使用 NSMutableString 代替 NSString 会减少一些开销.

Instead of NSString use NSMutableString some over head will be reduced.

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

这篇关于UITextView 中的替换文本运行缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:47