ios - 如何在 Objective-C 中使用 for 循环替换 HTML 值?

标签 ios objective-c uiwebview

我创建了 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;

}

在我的 WebView 中仅显示最后一个值。我想在 WebView 中显示所有替换的值。

我编辑的代码::

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 数组,给它们更好的名字!

关于ios - 如何在 Objective-C 中使用 for 循环替换 HTML 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14644445/

相关文章:

javascript - 在我的 iOS 应用程序中集成 jquery-javascript 项目

ios - 如何判断 UIWebView 内容何时更改?

ios - 我如何从 coreData swift 获取用户最近的聊天记录

objective-c - 如何从 NSMutableArray 子类接收通知

objective-c - 在事件 UITextField 之外的触摸上调用函数

ios - 如果设备被拔掉,解析无声推送通知不工作

ios - 在 uiimageview 中动画图像

ios - 为什么 iOS 在应用更新时会得到一个新的 identifierForVendor?

iphone - 如何告诉 UICollectionView 预加载更大范围的单元格?

iphone - iOS UIWebView 问题