【问题标题】:how to display the parsed json using textfield on table view如何在表格视图上使用文本字段显示解析的 json
【发布时间】:2011-06-22 09:35:06
【问题描述】:

嗨,我是 iphone 开发新手

我正在使用 web 服务连接我获取 json 响应字符串的位置,我正在使用 JSONvalue 解析 json responsetring,我正在使用按钮在标签上显示解析的 json...

当我单击按钮时,解析后的 json 将显示在标签字段上..

我的问题是可以在 UI 中有一个文本字段、按钮和 webview 或 tableview..

当我在文本字段中输入特定方法时,因为 json 文件很大,然后按下按钮 shd 在 webview 或 tableview 上显示特定方法的内容..

这可能吗?如果是,那么如何实现?

谢谢你

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{


     [connection release];    

    NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];


    self.dataWebService = nil;

    NSArray* latestLoans = [(NSDictionary*) [responseString JSONValue] objectForKey:@"loans"]; 
    [responseString release];    


    NSDictionary* loan = [latestLoans objectAtIndex:0];

    //fetch the data

    NSNumber* fundedAmount = [loan objectForKey:@"funded_amount"];

    NSNumber* loanAmount = [loan objectForKey:@"loan_amount"];

    float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];

    NSString* name = [loan objectForKey:@"name"];

    NSString* country = [(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"];

    //set the text to the label
    label.numberOfLines = 0;

    label.text = [NSString stringWithFormat:@"Latest loan: %@ \n \n country:  %@ \n \n µamount $%.2f", name,country,outstandingAmount];

【问题讨论】:

  • 您能否尝试重新表述您的问题?我真的不明白你的意思。
  • @Nayefc 实际上,目前我在 UI 中有一个按钮和一个标签,当我按下 Button 时,它会在 LABEL 中显示结果。即,它从 JSON 文件中获取并使用解析 json 显示在 LABEL 上。 .Actually我的要求是我在UI中有一个Textfield,button和一个webview......当我在TextField中输入一个字符串时,它会从JSon文件中搜索,例如上面代码中的“loans”......它的内容shd显示在 webview 中……你明白了吗?

标签: iphone objective-c ios xcode json


【解决方案1】:

如果您想将贷款放在表格中,您应该阅读UITableViewCell Styles

您将添加您的表格,然后指出部分的数量、每个部分中的行、每个单元格(行)的内容,以及如果其中一行被触摸时该怎么办:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //after turning latestLoans into a property
    return self.latestLoans.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text=[[self.latestLoans objectAtIndex:indexPath.row] objectForKey:@"name"];
    cell.detailTextLabel.text=[[self.latestLoans objectAtIndex:indexPath.row] objectForKey:@"loan_amount"];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //do something with the row that was selected, like load a table of detailed information
}

【讨论】:

  • webview 怎么样让你想用它作为显示选项?您可以创建一个方法来获取 JSON,循环遍历所有结果,将其转换为 HTML 并显示...是您想要的吗?
  • woods yup 你 r rite u hv 向我展示了如何在 tabelview 上做非常感谢..这对我有帮助.. 但我想要在 UI 中有一个文本字段按钮和 webview 的代码,在哪里我们在文本字段中输入一个字符串 tat shd 与 json 文件中的字符串匹配,然后 shd 在 webview 上显示匹配的方法...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多