2014-06-24 68 views
0

只需要練習一些練習代碼就可以了。我正在編寫一個應用程序,用戶在UITextView中輸入文本,按下按鈕,輸入的每個單詞都會更改爲由程序定義的單個單詞,從而保持標點符號不變。將用戶輸入的文本更改爲UITextView

這將如何實現?我最初想到計算單詞並簡單地替換它們,但是當標點符號消失時不起作用。

有什麼想法?

謝謝,

回答

3

這是我的解決方案:

NSString *inputString = @"Replace all the words! Will you?"; 
NSString *substitutionString = @"bla"; 
NSMutableString *outputString = [inputString mutableCopy]; 

[outputString enumerateSubstringsInRange:(NSRange){0, [outputString length]} options:NSStringEnumerationByWords usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { 
    [outputString replaceOccurrencesOfString:substring withString:substitutionString options:0 range:substringRange]; 
}]; 

NSLog(@"%@", outputString); 
+0

好多了!我現在感到很蠢...... :) – dasdom

+0

當然,我指出了我正確的方向,絕對比我想要做的更容易的解決方案!謝謝你,先生。 – mattFllr

-1

是的,這是細胞可重用性的問題,嘗試創建像這樣的細胞標識符。它會工作。

NSString *identifier =[NSString stringWithFormat:@"%d_%d",indexPath.row,indexPath.section]; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 
    } 

如果您使用自定義單元格,單元格創建代碼將相應地更改。

+0

感謝流星,但我不使用的tableview,只有UITextView的。 – mattFllr

相關問題