2

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を1週間試してみましたが、それでもスタックします。私の場合、それを使用して交換する方法がわかりません。

案内してください!前もって感謝します!それは緊急の課題です。

4

2 に答える 2

0

不変の文字列を使用していて、非常に長い文字列を作成して、すでに作成されている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;
}
于 2013-03-21T06:22:54.317 に答える
0

いいえ、UITextViewを使用する場合にこれ以上の方法はありません(ios6以下では機能しません)

iOS7にはテキストキットがあります

于 2014-01-24T13:02:14.240 に答える