试试这样:
首先从您在NSMutableArray 中的回复中获取您想要的所有值
@interface ViewController ()
{
NSMutableArray *restaurentsNamesArray;
NSMutableArray *linesArray;
NSMutableArray *linesCountArray;
}
在viewDidLoad 之后,将值添加到您从响应中获得的那些可变数组中
- (void)viewDidLoad {
[super viewDidLoad];
restaurentsNamesArray = [[NSMutableArray alloc]init];
linesArray = [[NSMutableArray alloc]init];
linesCountArray = [[NSMutableArray alloc]init];
NSError *error;
// Do the stuff for get response from url here.
// And give that request below.
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSDictionary *main = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSDictionary *result = [main valueForKey:@"result"];
NSArray *saleorders = [result valueForKey:@"saleorders"];
for (NSDictionary *dict in saleorders){
[restaurentsNamesArray addObject:[dict valueForKey:@"restaurant_name"]];
[linesArray addObject:[dict valueForKey:@"lines"]];
}
NSLog(@"%@",restaurentsNamesArray);
NSLog(@"%@",linesArray);
}
现在你想要的只是实现如下表视图委托方法:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [restaurentsNamesArray count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *array;
if (linesArray.count > section){
array = [linesArray objectAtIndex:section];
}
return array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc]init];
NSArray *array;
array = [linesArray objectAtIndex:indexPath.section];
NSDictionary *dict = [array objectAtIndex:indexPath.row];
cell.textLabel.text = [dict valueForKey:@"line_name"];
cell.detailTextLabel.text = [dict valueForKey:@"line_id"];
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [restaurentsNamesArray objectAtIndex:section];
}
在这里,我只是将tableView 部分标题中的餐厅名称填充为NSString。
如果你想要和上面展示的完全一样的android,你必须实现下面的方法而不是-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section这个方法
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 50)];
[headerView setBackgroundColor:[UIColor darkGrayColor]];
UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake((headerView.frame.size.width/2)-47,-32,300,94)];
tempLabel.backgroundColor=[UIColor clearColor];
tempLabel.shadowColor = [UIColor blackColor];
tempLabel.shadowOffset = CGSizeMake(0,2);
tempLabel.textColor = [UIColor whiteColor];
tempLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
tempLabel.font = [UIFont boldSystemFontOfSize:17.0];
tempLabel.text= [restaurentsNamesArray objectAtIndex:section];
[headerView addSubview:tempLabel];
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
return 35;
}
就像我在cell.textLabel 中填充lineName 一样
如果您想做更多,只需创建自定义tableViewCell 并根据需要创建布局。
就是这样。
干杯。