簡體   English   中英

如何在Objective-C中使用for循環替換HTML值?

[英]How to replace HTML values using for loop in Objective-C?

我創建了html文件,並替換了html文件中的一些值。 沒事。 但是我想使用for循環和替換值。 但是我的問題是它僅替換最終值。

我的html代碼:

<body bgcolor="#F5DA81">
<h1><center>Simple Report</center></h1>
<p> This report contais about comapny details according to contact details.</p>
<center><b><u><h3>Company Details</h3></u></b></center>
    <table>
        <tr><td>Company Name:</td><td><!--company--> </td></tr>
        <tr><td>Company Id  :</td><td><!--companyId--></td></tr>
    </table>
<center><b><u><h3>Contact Details</h3></u></b></center>
    <table>
        <tr><td>Contact Name:</td><td><!--contact--></td></tr>
        <tr><td>Contact Id  :</td><td><!--contactId--></td></tr>
    </table>
</body>

我的for循環數組替換值。

NSMutableArray *valueArray = [[NSMutableArray alloc]init];
NSString *va = @"NU";
NSString *va1 = @"My Name";
NSString *va2 = @"WEB";
NSString *va3 = @"My Web System";

[valueArray addObject:va];
[valueArray addObject:va1];
[valueArray addObject:va2];
[valueArray addObject:va3];

NSMutableArray *myValue = [[NSMutableArray alloc]init];
NSString *val = @"<!--company-->";
NSString *val1 = @"<!--companyId-->";
NSString *val2 = @"<!--contact-->";
NSString *val3 = @"<!--contactId-->";

[myValue addObject:val];
[myValue addObject:val1];
[myValue addObject:val2];
[myValue addObject:val3];

NSMutableString *html1=[[NSMutableString alloc]init];

    for(int i = 0 ; i < 4; i++) {

        NSString *myFinalString = [myValue objectAtIndex:i];
        NSString *myVal = [valueArray objectAtIndex:i];
        html1 =[self HtmlReportName:@"myFirst" withReplaceId:myFinalString withValue:myVal];
        i = i + 1;
    }    

    [webView loadHTMLString:html1 baseURL:[[NSBundle mainBundle] resourceURL]];

HTML替換方法。

-(NSMutableString *) HtmlReportName:(NSString *)reportName withReplaceId:(NSString *)replaceId withValue:(NSString *)value {
NSMutableString *html;
html = [NSMutableString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:reportName ofType:@"html"] encoding:NSUTF8StringEncoding error:nil];
[html replaceOccurrencesOfString:replaceId withString:value options:0 range:NSMakeRange(0, [html length])];
return html;

}

在我的網絡視圖中僅顯示最后一個值。 我想在網絡視圖中顯示所有替換的值。

我的編輯代碼::

NSMutableString *html12 = [NSMutableString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myFirst" ofType:@"html"]                                                           encoding:NSUTF8StringEncoding error:nil];

for (NSUInteger i = 0; i < [valueArray count]; i++) {

    NSString *myFinalString = [myValue objectAtIndex:i];
    NSString *myVal= [valueArray objectAtIndex:i];
    [html12 replaceOccurrencesOfString:myVal
                          withString:myFinalString
                          options:0
                          range:NSMakeRange(0, [html12 length])];
}

 [webView loadHTMLString:html12 baseURL:[[NSBundle mainBundle] resourceURL]];

問題是您沒有將經過修改的HTML傳遞回下一個替換,而是每次都重新加載原始HTML,因此丟失了先前的替換。

嘗試這個:

NSMutableString *html = [NSMutableString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:reportName ofType:@"html"]
                                                         encoding:NSUTF8StringEncoding
                                                            error:nil];
NSAssert([myValue count] == [valueArray count], @"Doh!");
for (NSUInteger i = 0; i < [valueArray count]; i++)
{
    NSString *myFinalString = [valueArray objectAtIndex:i];
    NSString *myVal= [myValue objectAtIndex:i];
    [html replaceOccurrencesOfString:myVal
                          withString:myFinalString
                             options:0
                               range:NSMakeRange(0, [html length])];
    // Remove i=i+1 !!!
}    

並轉儲毫無意義的HtmlReportName方法。

注意,您是從前到后使用valueArraymyValue數組的, 給它們更好的名稱!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM