【问题标题】:collapse expand header section using json file使用 json 文件折叠展开标题部分
【发布时间】:2013-03-23 12:52:16
【问题描述】:

其实我是iOS编程新手,我正在努力自学。

我有一个带有标题部分的分段表视图,但我希望该视图能够在点击此部分时折叠展开部分中的行。

我用这个教程:http://blog.paxcel.net/blog/expandablecollapsible-table-for-ios/

但在我的应用程序中,我有一个 Json 文件而不是 Plist 文件。

所以我在 setCategoryArray() 函数中使用:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
                [NSURL URLWithString: @"http://......./catjsonf.php"]];

NSError* error;
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

jsonResults = [json objectForKey:@"nodes"];
dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableView reloadData];
});


});

而不是:

  NSURL *url = [[NSBundle mainBundle] URLForResource:@”CategoryList” withExtension:@”plist”];

NSArray *mainArray = [[NSArray alloc] initWithContentsOfURL:url];

但它不起作用 - 我应该做点别的吗?

【问题讨论】:

  • 您正在从 web 服务获取数组。需要花时间。使用异步方法并在connectionDidFinishLoading 中重新加载您的表。

标签: iphone ios json uitableview


【解决方案1】:

查看我的博客,它一定会对您有所帮助。这是链接。

Expandable/Collapsible Table For iOS

你需要做以下事情

  1. 首先根据您的 json 文件设计您的类,在我的例子中是“类别”。
  2. 完成模型类后,您需要编辑以下方法来准备模型对象数组。

提示:从json中,我们可以直接找到我们的字典,比如

id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

可以进一步转换结果以获取字典对象。而加载数据就像下面的方法一样

    - (void) setCategoryArray
    {

          NSURL *url = [[NSBundle mainBundle] URLForResource:@”CategoryList” withExtension:@”plist”];

          NSArray *mainArray = [[NSArray alloc] initWithContentsOfURL:url];

          NSMutableArray *categoryArray = [[NSMutableArray alloc] initWithCapacity:[mainArray count]];

         for (NSDictionary *dictionary in mainArray) 
         {

              Category *category = [[Category alloc] init];

              category.name = [dictionary objectForKey:@"name"];

              category.list = [dictionary objectForKey:@"list"];

              [categoryArray addObject:category];

          }

         self.categoryList = categoryArray;
     }
  1. 完成此操作后,唯一剩下的就是编辑表格视图的数据源并相应地委托方法。

【讨论】:

  • 我已经基于此代码构建了我的应用程序,但问题是我有一个 Json 文件而不是 plist 文件。你能帮我吗,我该如何实现它。
  • :P...你之前用过我的博客我没注意到。那太棒了。谢谢。将回复您并提供解决方案。请给我一些时间。
  • 您的数据源是什么并不重要。一旦你准备好你的模型数组。您在该基础上加载表。在我的实际应用程序中,我只从 json 加载数据。
  • 我相信,你会帮助我,因为我喜欢你的工作,你会解决我的问题
  • 我在等待你的支持 :)
猜你喜欢
  • 2015-06-27
  • 2021-12-01
  • 1970-01-01
  • 2023-03-20
  • 2013-06-19
  • 1970-01-01
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
相关资源
最近更新 更多