我有一个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/