Siva Sankar Siva Sankar - 1 year ago 74
iOS Question

Custom tableview cell multiple rows

This is samole image
This is my json data. Here it is Restaurant name coming one and line name coming 2 some times line name coming more then how to print the data in custom cell.please. help me

"currency": "$",
"state": "sale",
"total": 243.1,
"name": "a1238",
"restaurant_name": "\"Food Court\" Biergarten",
"date": "2016-10-16 07:52:07",
"table_no": null,
"so_id": 238,
"lines": [
"line_status": "pending",
"line_id": 2536,
"line_price": 1,
"line_qty": 1,
"line_name": "Käse"
"line_status": "pending",
"line_id": 2579,
"line_price": 7.8,
"line_qty": 2,
"line_name": "3 Musketiere (3x verschiedene Hefe 0,3l)"

Answer Source

Try like this:

First get all values from your response in NSMutableArray what you want

@interface ViewController ()
    NSMutableArray *restaurentsNamesArray;
    NSMutableArray *linesArray;
    NSMutableArray *linesCountArray;

After that in viewDidLoad add values to those mutable arrays which you got from your response

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


Now all you want is implement table view delegate methods like below:

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

Here I'm just populating restaurant names in tableView section header as NSString.

If you want exactly like android what you shown above you have to implement below methods instead of -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section this method

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

Like the same way I'm just populating lineName in cell.textLabel

If you want to do more , just create custom tableViewCell and create layout how you want.

that's it.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download