1

私はiphoneアプリを持っていますコンボボックスを作成しましたコンボボックスがhtmlファイルからではなくxcodeから直接値を取得する必要があるので、次のコードを使用してhtmlファイルでコンボボックスを取得しています

NSString *htmlPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"combo.html"];
NSString *htmlContent = [NSString stringWithContentsOfFile:htmlPath];
[webView loadHTMLString:htmlContent baseURL:nil]; 

コンボ.html:

 <html>   
 <SELECT NAME="food" SIZE="10" style="width: 200px;" style="height: 100px "> 
 <OPTION VALUE="0">OK</OPTION>
 <OPTION VALUE="1">Good</OPTION>
 <OPTION VALUE="2">Best</OPTION>
 <OPTION VALUE="3">Average</OPTION>
 </SELECT> 
 </html>
4

1 に答える 1

0

質問を完全に理解しているかどうかはわかりませんが、アプリ バンドルから HTML ファイルを読み込んで、アプリから別の HTML コンテンツを動的に挿入できるかどうかを尋ねていると思います...そうですか?

もしそうなら、あなたは確かにそれを行うことができます。

まず、コンテンツを変更可能な文字列にロードします

NSString *htmlPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"combo.html"];
NSError *error;
NSMutableString *htmlContent = [NSMutableString stringWithContentsOfFile: htmlPath 
                                                                encoding: NSUTF8StringEncoding 
                                                                   error: &error];

コンボ ボックスに新しいオプション/値を挿入するには、次のようにします。

// look for the start of the combo box called "food"
NSRange range = [htmlContent rangeOfString: @"select name=\"food\"" 
                                   options: NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
    // search for the end tag </select>
    range.length = htmlContent.length - range.location;
    NSRange end = [htmlContent rangeOfString: @"</select>" 
                                     options: NSCaseInsensitiveSearch 
                                       range: range];
    if (end.location != NSNotFound) {
        NSString *newChoice = @"Awesome!";  // get this dynamically however you want
        NSString *newOption = 
           [NSString stringWithFormat: @"<option value=\"4\">%@</option>\n", newChoice];
        [htmlContent insertString: newOption atIndex: end.location];
        NSLog(@"htmlContent = %@", htmlContent);
    }
}

既存のコンボ ボックス オプションの表示値を変更する場合は、次のようなコードを使用します。

NSString *optionTwoHtml = @"<option value=\"2\">";
NSRange optionTwo = [htmlContent rangeOfString: optionTwoHtml 
                                       options: NSCaseInsensitiveSearch];
if (optionTwo.location != NSNotFound) {
    int start = optionTwo.location + optionTwoHtml.length;
    // search for the end tag </option>
    optionTwo.length = htmlContent.length - optionTwo.location;
    NSRange end = [htmlContent rangeOfString: @"</option>" 
                                     options: NSCaseInsensitiveSearch 
                                       range: optionTwo];
    if (end.location != NSNotFound) {
        NSString *newValue = @"Better Than Best!";
        NSRange oldRange = NSMakeRange(start, end.location - start);
        [htmlContent replaceCharactersInRange: oldRange 
                                   withString: newValue];
        NSLog(@"htmlContent = %@", htmlContent);
    }
}

このコードは、何ができるかを示しているだけであることに注意してくださいパフォーマンスのために最適化されていません。あなたが示した HTML コンテンツは非常に小さいので、解析コードがどれほど効率的であるかは問題ではありません。はるかに大きな HTML ファイルを使用する場合は、少し最適化することをお勧めします。

于 2012-07-21T08:20:03.893 に答える