【问题标题】:How to set numberOfSectionsInTableView, titleForHeaderInSection using dynamic json data如何使用动态 json 数据设置 numberOfSectionsInTableView、titleForHeaderInSection
【发布时间】:2012-10-03 07:37:16
【问题描述】:

我有一些下面给出的 json 数据,我想将其显示在一个表中,其中部分标题是部门、设计、员工、开发、员工。

我在numberOfSectionsInTableViewtitleForHeaderInSection 中使用了静态数据,但我想使用动态数据。

我该怎么做?

{                                                                                                                                                                          
    "Departments":[
        {
            "name":"Designing",
            "id":"1.1",
            "Employees":[
                {
                    "name":"Ramesh",
                    "id":"1.1.1",
                    "salary":"4lakhs"
                },
                {
                    "name":"Suresh",
                    "id":"1.1.2",
                    "salary":"4lakhs"
                }
            ]
        },
        {
            "name":"Developing",
            "id":"1.2",
            "Employees":[
                {
                    "name":"Ram",
                    "id":"1.2.1",
                    "salary":"4lakhs"
                },
                {
                    "name":"Sam",
                    "id":"1.2.2",
                    "salary":"4lakhs"
                }
           ]
      }
}

【问题讨论】:

  • 过滤您的 json comming 数据并使用 Dictionary and and Key 添加到 NSMutable 数组中
  • 在这个你想在tableview中显示什么?你想显示的部门名称..?
  • "Departments" 是第一个标题部分标题,然后 "Designing" 是第二个标题部分标题,然后 "employees" 是第三个标题部分标题,然后将有 3 行 2 行包含员工数据.. 然后“开发”是第 4 个标题部分标题,反之亦然...我已成功显示所有内容,但正如我在上述问题中所问的,我应该使用动态数据而不是静态数据
  • 谁能给我一个完美的解决方案...?
  • 我需要更好的答案......

标签: iphone objective-c ios json


【解决方案1】:

首先你必须从你指定的字符串 JSON 对象创建一个 NSDictionary。

    NSDictionary *myDict = [NSJSONSerialization JSONObjectWithData:webData options:nil error:NULL];

然后在表格视图中委托方法:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return [[myDict valueForKey:@"Departments"] count];
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [[[[myDict valueForKey:@"Departments"] objectAtIndex:section] valueForKey:@"Employees"] count];
    }

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        return [[[myDict valueForKey:@"Departments"] objectAtIndex:section] valueForKey:@"name"];
    }

或者至少我会尝试这样设置它们。

【讨论】:

  • 呃...等等,也许您想要一些不同的东西,比如部门的名称...让我编辑我的答案
  • @Deboroh88.. 先生,您的回答似乎很完美......但我也在显示员工的姓名......在 cellForRowAtIndexPath 我使用了 NSDictionary *dict=[self.data objectAtIndex:indexPath.排]; switch (section) { case 2: cell.textLabel.text= [dict objectForKey:@"name"];休息;案例 4:cell.textLabel.text= [dict1 objectForKey:@"name"];休息;
  • @Deboroh88.. 你能回复我吗...?
  • 呃……我在理解这段代码时遇到了一些问题……我的意思是,什么是“dict1”?要获取员工的姓名,我将使用 "[[[[[myDict valueForKey:@"Departments"] objectAtIndex:indexPath.section] valueForKey:@"Employees"] objectAtIndex:indexPath.row] valueForKey:@"name"] ;"
  • 但是,阅读你对这个问题的最后评论,我知道你需要一些不同的东西......在这种情况下,如果你想要你描述的所有标题(我认为其中一些是无用的,像“员工”),它是编辑整个答案。 (如果我现在理解正确的话......)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
相关资源
最近更新 更多