0

我在下面给出了一些 JSON 数据,我想在表格视图中显示它。我已经成功地使用了静态数据,但现在我想使用动态数据来做到这一点。我怎样才能做到这一点?

4

5 回答 5

0

它非常简单,使用 JSON Parser 解析 JSON 数据并将数据存储在NSMutableArray其中并将该数组传递给您的表视图。希望这可以帮助你..

于 2012-10-04T13:46:08.693 回答
0

您必须使用 JSON 解析器来构建您可以正常操作的数据结构(通常是嵌套的 NSArray、NSDictonary、NSNumber 和 NSString 实例)。请参阅我的解决方案。

旁注:如果您不熟悉从非结构化数据创建结构化数据这样的基本概念,那么您应该学习熟悉此类内容,而不是已经制作了The Most Bestest iPhone App Yet (TM)。

于 2012-10-04T13:47:50.550 回答
0

一旦您下载并解析了 JSON 数据,您就可以有效地获得静态数据。您可以在显示表格视图之前下载并解析 JSON 数据,然后使用数组或字典来指定您希望表格视图的布局方式。您需要以自己的方式将 JSON 数据表示为值对象,然后您可以对其进行计数和读取,以向表格视图提供所需的数据。如果您要在显示表格视图后获取数据,那么一旦您有了数据,只需调用-[<tableViewName> reloadData]

于 2012-10-04T13:49:01.347 回答
0

您可以创建具有背景颜色和文本对齐的标题。

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

  UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
if (section == 0){
   [headerView setBackgroundColor:[UIColor redColor]];
    UILabel * headerLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, -5, headerView.frame.size.width, headerView.frame.size.height)];

    headerLabel.textAlignment = UITextAlignmentCenter;
    headerLabel.text =@"Current Schedule";
    [headerLabel setFont:[UIFont fontWithName:@"Arial-ItalicMT" size:18]];
    headerLabel.backgroundColor = [UIColor clearColor];

    [headerView addSubview:headerLabel];
    [headerLabel release];

    return headerView;
}

}

于 2012-10-04T13:50:40.380 回答
0
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {

return [[[[jsonObject valueForKey:@"companies"] objectAtIndex:0] valueForKey:@"Departments"]count];
 }

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



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

label1.text = [[[[[[[jsonObject valueForKey:@"companies"] objectAtIndex:0] valueForKey:@"Departments"]objectAtIndex:section]valueForKey:@"Employees"]objectAtIndex:row]valueForKey:@"name"];

  }
  - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
     {
     return [[[[[jsonObject valueForKey:@"companies"] objectAtIndex:0] valueForKey:@"Departments"]objectAtIndex:section]valueForKey:@"name"];
  }
于 2012-10-11T10:34:53.517 回答