Ashish Gabani
Ashish Gabani

Reputation: 443

How to Set a Space between Tableview Cell in iOS?

I am newbie in iOS development and i know this question is asked many times but i confuse for it. I want to know how to set a space between UITableview cell in section.In my app UITableview contain two section first section contain only one data value so not any problem. but my second section contain 5 to 7 data value But not a space between them how to set a space in footer between second section cell in UITableview.

Upvotes: 1

Views: 1334

Answers (1)

Magoo
Magoo

Reputation: 2619

OK so you have two methods for sections

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

and

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

but neither serve the purpose of putting space between cells in a single section.

For my money there are two options... first and most complicated / hacky

  1. in - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView you could return the count of the cells in the second section and in cellForRowAtIndexPath if (section == 0) put the data in the usual way... else you can pull the info out of the array or whatever using [theArray objectAtIndex:indexPath.section] instead.

  2. (much simpler, and better practice).. Create a UITableViewCell subclass and use that in your cellForRowAtIndexPath like so

    (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    static NSString *MyCellIdentifier = @"MyCustomCell";
    
    switch (indexPath.section) {
        case 0:
        {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (!cell)
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
        // setup your 1st section cells here
    
        return cell;
    
        }
            default:
        {
            MYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];
    
            if (!cell)
                cell = [[MYTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier];
    
           // all other layout code here if indexPath.row == 0 etc.
    
           return cell;
        }
    }
    

in your custom cell .m you can set

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(0,0,0,0); // lay them out at the top / middle / wherever  in your cell
    self.textLabel.frame = CGRectMake(0,0,0,0); // which will allow for some space at the bottom / edges
}

then finally use

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

   if(indexPath.section == 0)
     return THE_CELL_HEIGHT;
   else 
     return THE_CELL_HEIGHT + PADDING;
}

This way you can set padding in the cell itself, which is cleaner and reusable. If you want different colours to mark the spacing, you should create UIViews in the custom UITableViewCell subclass method

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

and add them using [self.contentView addSubView:someView]; you can always set the backgroundColor of a cell to [UIColor clearColor]; if you have an image / content behind the tableView

Upvotes: 2

Related Questions