Adding background for TableView sections iOS

645 Views Asked by At

I have a section with dynamic multiple rows and I need to separate all the section with the background as shown in the image below.

Specifically those border lines for all the section. Pease let me know how can I make it possible.

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = [ProductArray objectAtIndex:section];
    return arr.count;    
}

Please refer to this

image.

I tried adding header and footer, But I am not understanding how to add that rectangle box image for the entire section.

2

There are 2 best solutions below

0
On

Put View in your cell after make outlet of that view. And use to cellForRowAtIndexPath this method.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];
                [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    cell.viewBackGround.backgroundColor = [UIColor redColor];
    return cell;
}
0
On

For above requirement, I would like to suggest you the below approach.

create only one section with multiple rows.

  1. Each row of UITableView will have UITableViewCell
  2. Inside UITableViewCell create content view for which we are gonna add the border
  3. Each row of UITableViewCell will have another UITableView(inside content view) whose methods are handled by UITableViewCell cells
  4. Change UITableView (inside) height based on number of rows.
  5. provide height of UITableViewCell based on the number of rows + orderID Header.