簡體   English   中英

以動態方式在UITableView中打印JSON數據

[英]to print JSON data in UITableView in dynamic way

我在下面提供了一些JSON數據,我想在表格視圖中顯示它。 我已經成功使用了靜態數據,但是現在我想使用動態數據來完成。 我怎樣才能做到這一點?

它非常簡單,使用JSON解析器解析JSON數據並將數據存儲在NSMutableArray然后將該數組傳遞到表視圖中。 希望這對您有幫助。

您必須使用JSON解析器來構建可以正常操作的數據結構(通常是嵌套的NSArray,NSDictonary,NSNumber和NSString實例)。 請參閱我的解決方案。

旁注:如果您不熟悉諸如從非結構化數據中創建結構化數據這樣的基本概念,則應該學習熟悉此類內容,而不是已經制作了“最好的iPhone App Yet(TM)”。

一旦下載並解析了JSON數據,就可以有效地擁有靜態數據。 在顯示表視圖之前,您可以下載並解析JSON數據,然后使用數組或字典指定表布局的方式。 您將需要以自己的方式將JSON數據表示為值對象,然后可以對其進行計數和讀取以向表視圖提供所需的數據。 如果您要在顯示表視圖后獲取數據,則一旦獲得數據,只需調用-[<tableViewName> reloadData]

您可以創建具有背景色和文本對齊方式的標題。

-(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;
}

}

  - (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"];
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM