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.
-
You want a space between the cells or the sections ? – Zigglzworth Nov 04 '14 at 12:38
-
use cutom view in the place of cell – Anbu.Karthik Nov 04 '14 at 12:39
-
See Flea's answer here: http://stackoverflow.com/questions/6216839/how-to-add-spacing-between-uitableviewcell – Zigglzworth Nov 04 '14 at 12:40
-
@Zigglzworth i want a space between cells and this cells are in my TableView second section. – Ashish Gabani Nov 04 '14 at 12:43
-
see my comment above – Zigglzworth Nov 04 '14 at 12:43
-
Bro i wrote this method but it apply for Section not section cell -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { if (section == 0) { return 30; } else if (section == 1) { return 5; } return 0; } please give me solution if you know. – Ashish Gabani Nov 04 '14 at 12:45
1 Answers
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
in
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
you could return the count of the cells in the second section and incellForRowAtIndexPath
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.(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

- 2,552
- 1
- 23
- 43