2017-07-14 38 views
0

请教我如何显示树形视图等数据。uitableview如何动态解析数据的多个单元格

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return marrSearchResult.count; 

} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    int iCount=0; 

    if (section==0) { 

     iCount =(int)[marrSearchResult count]; 
    }else if(section==1){ 

     iCount =(int)[[[[marrSearchResult objectAtIndex:section] valueForKey:@"data"] valueForKey:@"result"] count]+1; 
    }else{ 

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

    static NSString *simpleTableIdentifier = @"MyTravelProfileTblCELL"; 

    if (indexPath.section == 0) { 
    }else{ 
    } 
} 

看到图像desplay下面这样 enter image description here

+0

你能告诉我们你得到即marrSearchResult的数据? – luckyShubhra

+0

[ { “标题”: “娱乐”, “数据”:[ { “type_title”: “餐馆”, “结果”:[ { \t \t \t “place_data”:[ { “name”:“Tim Hortons”, “address”:“1910 Yonge St”, “number”:“+1 416-481-0209”, “website”:“www.google.com” } ] } ] } ] } ] –

+0

@luckyShubhra请检查以上阵列 –

回答

2

遵循以下步骤:

  • 创建节头的自定义的tableview细胞(旅游景点,政府/社区)。让我们称之为PlaceTypeCell
  • 为行(博物馆,医院...)创建另一个自定义表格单元格。我们称之为PlaceNameCell。
  • 返回区段计数作为主阵列的数量(你已经做到了)
  • 返回的行数作为子阵列的部分的主阵列中(你也做到了)指数
  • 现在伯爵在cellForRowAtIndexPath中,根据节号(indexPath.section)从main获取子数组。现在使用行号(indexPath.row)从子数组中获取所需的行项目(地点名称和详细信息)。使用该行项目中的数据填充表格单元格。
  • 现在在viewForHeaderInSection中使用节号从主数组中获取地点类型详细信息并填充PlaceTypeCell并返回为节标题。看看Best way to create custom UITableview section header in storyboard
  • 设置使用heightForHeaderInSection

希望它可以帮助为节头的高度。

编辑:如果您可以显示marrSearchResult的格式,那么它将很容易帮助。

1

在格式创建输入数组:

NSArray *tourismSection = @[@"Museums", @"Aquariums"]; 
NSArray *govSection = @[@"Hopsital"]; 

NSDictionary *data = @{ 
    @"tourism" : tourismSection, 
    @"gov"  : govSection 
}; 
NSArray *inputs = @[@"tourism",@"gov" ]; 

numberOfSectionsInTableView

​​ 在 numberOfRowsInSection

return [data valueForKey:inputs[indexPath.section]].count;