【问题标题】:How to display the json's Mutable Array data like Expandable/Collapsible TableView in iphone?如何在 iphone 中显示 json 的可变数组数据,如可扩展/可折叠 TableView?
【发布时间】:2013-10-28 12:07:51
【问题描述】:

我是 IOS 新手。我从 WebService 获取数据并使用 json 序列化将该数据存储到 MutableArrays 中。但现在我必须在 TableView 中显示该数据,例如可扩展和可折叠单元格。请发给我解决这个问题的示例代码。

  NSMutableArray *arr=[NSMutableArray alloc]init];

    NSString *Str_Menu_desc=[NSString alloc]init];

    NSURL *url=[NSURL URLWithString:@"http://www.google.com/CategoryInfo.php"];

    NSData *data=[NSData dataWithContentsOfURL:url];

    NSError *err;

    json=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&err];


    NSEnumerator *enu=[json objectEnumerator];

    while (dic =(NSDictionary *)[enu nextObject])
    {

            [arr addObject:[dic1 objectForKey:@"menu_name"]];

    }


    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

{

    return [arr count];

}


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


{

    cell=[[UITableViewCell alloc]init];
    cell.textLabel.text=[arr objectAtIndex:indexPath.row];
    return cell;


}


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

{

    NSString *local=[arr objectAtIndex:indexPath.row];


       NSURL *url=[NSURL URLWithString:@"http://www.google.com/SubCategoryInfo.php"];

    NSData *data=[NSData dataWithContentsOfURL:url];

    NSError *err;

    json=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&err];


    NSEnumerator *enu=[json objectEnumerator];

    while (dic =(NSDictionary *)[enu nextObject])
    {
        if (local isEqual:[dic objectForKey:@"category_name"]])
        {

            Str_Menu_desc=[NSString stringWithFormat:@"%@",[dic objectForKey:@"Sub_Category_name"]];

        }
    }



}

现在,我使用选定的类别名称获得了子类别名称。我必须在可展开/可折叠的 TableView 中显示类别和子类别名称。提前致谢。

【问题讨论】:

    标签: objective-c ipad uitableview ios7


    【解决方案1】:

    您所说的可扩展-可折叠单元是什么意思?可能是这样的:Expanding/Collapsing TableView Sections?

    另一种方式,在 -tableView:tableView didSelectRowAtIndexPath: 方法中接收到的数据,放入新的 UITableViewController 并将其推送到视图层次结构中,代码如下:[self.navigationController pushViewController:content animated:YES]; 记住:必须先创建导航控制器,否则代码将不工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      • 2022-12-15
      相关资源
      最近更新 更多