How to give space between two cells in tableview?

93,608

Solution 1

You can create a Sections of TableView also in the UITableView... This methods are compulsory so create sections and in each section you can create single cell as in your picture..

Solution 2

you can't set distance between cells directly, but you can set the height for header in section to achieve the same result.

1.set the numbers of cell you need as sections:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 3; // in your case, there are 3 cells
}

2.return only 1 cell for each section

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

3.set the height for header in section to set space between cells

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.; // you can have your own choice, of course
}

4.set the header's background color to clear color, so it won't look weird

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}

Solution 3

The Best way to get space between two cells in TableView, declare the numbers of sections you want in delegate method of numberofsections this way

For example you have array of 10 objects

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return [array count]; //array count returns 10
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return 1;// this should be one because it will create space between two cells if you want space between 4 cells you can modify it.
}

Then the important point is in cellForRowAtIndexPath delegate method you need to use indexpath.section but not indexpath.row

cell.textLabel.text=[NSString stringWithFormat:@"%@",[array objectAtIndex:indexPath.section]];

That is is check your tableview for the space between two cells. Enjoy!

Solution 4

The multiple sections answer would work, but it's extremely brittle, and doesn't allow for actual sections. Instead, you should create a custom cell, or custom cell prototype that simply has a gap at the bottom and/or top.

Use your struts and springs in IB to maintain that uniform gap, and use heightForRowAtIndexPath to return a height that includes the gap.

Solution 5

Objective - C

 UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 3)];/// change size as you need.       
 separatorLineView.backgroundColor = [UIColor whiteColor];// you can also put image here
 [cell.contentView addSubview:separatorLineView];

Swift 3 (This same will work for Swift 4 also)

var separatorLineView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 3))
/// change size as you need.       
separatorLineView.backgroundColor = UIColor.white
// you can also put image here
cell.contentView.addSubview(separatorLineView)

It worked for me.

Share:
93,608
Ankit Chauhan
Author by

Ankit Chauhan

Updated on August 31, 2020

Comments

  • Ankit Chauhan
    Ankit Chauhan over 3 years

    I want a space between two cell in table view,

    I want cell like this,

    enter image description here

    How can i do that?

  • The iCoder
    The iCoder almost 12 years
    But how u will maintain that array which we are giving in the cellforrowindexpath ....i am facing problem that my every cell printing the same element in every section row. Any idea??
  • DShah
    DShah almost 12 years
    @PavanMore: What is your exact scenario?? It is easy to maintain array. Just declare the property of NSArray and initialize it. Then based on cellForRowIndexpath i.e. Index of cell just change the cell text. its easy... if your scenario is different then please elaborate it.
  • Douglas
    Douglas about 11 years
    Thanks for this tidbit of info. Worked like a charm!
  • iPhone Programmatically
    iPhone Programmatically about 11 years
    It is good, but distance can be created without using this way also. link "stackoverflow.com/questions/11453366/…".
  • n13
    n13 about 10 years
    It's not brittle - I don't think - but it's kinda hackish. I've made the gap part of the cell in my solution before finding this page but it comes with drawbacks too. In my case it makes the cell design much more complicated than it would be if the gap were managed by the table. YMMV.
  • denikov
    denikov about 10 years
    Thanks for the indexPath.section part
  • Mubin Mall
    Mubin Mall about 10 years
    How to give particular space like 5px or 10px between them
  • obaid
    obaid almost 10 years
    You can set the header and footer height of sections
  • mask
    mask almost 10 years
    How do you calculate number of row in a single section table
  • Khairulnizam Dahari
    Khairulnizam Dahari almost 10 years
    Based on your content array. Eg: -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [menus count]; }
  • WaaleedKhan
    WaaleedKhan over 9 years
    An Excellent Approach.
  • JRam13
    JRam13 about 9 years
    If you have both multiple sections and rows, try this way stackoverflow.com/a/29258935/2446178
  • Nikunj Jadav
    Nikunj Jadav about 9 years
    Its not working. For exmaples arrays count 10 and when I have use this code in cellForRowAtIndexPath and heightForRowAtIndexPath but its return only 5 rows because I think based on this condition if (indexPath.row % 2 == 1).
  • Tyler
    Tyler almost 9 years
    this makes the editing actions look like crap, the pseudo margin is actually a part of the cell, so in the case you have editing actions, the action view appears to be taller than the view
  • Alex Lacayo
    Alex Lacayo almost 9 years
    I would recommend doing header.userInteractionEnabled = false so the clear header view does not receive touches. In case the view is overlapping a button.
  • Rajal
    Rajal almost 9 years
    This is easiest way,I found so far,but if you need to use sections and rows and avoid overload of extra ones,try adding elements on UIView,instead directly in content view.Also,add constraints accordingly.
  • Nuno Gonçalves
    Nuno Gonçalves over 8 years
    I was gonna come here to answer this, as it was my approach. Very simple.
  • plamut
    plamut over 8 years
    Please just fix the fonts a bit, no need to use bold and big letters, thanks. :)
  • plamut
    plamut over 8 years
    No worries at all @PeiweiChen, we were all new here at some point. And thanks for your contribution!
  • Suragch
    Suragch over 8 years
    This answer was very helpful for me. Here is a Swift version.
  • Admin
    Admin about 8 years
    Great but it will add header for first cell too, also it will not allow to pass data from an array in cellForRowAtIndexPath since all the cells will be filled with the first element of the array.
  • Husam
    Husam about 8 years
    You should give a higher height in heightForRowAtIndexPath then add inset @UtkuDalmaz
  • Brian
    Brian about 8 years
    If you don't want to add header to the first cell, just add a if/else statement in heightForHeaderInSection. For the array issue, just use indexPath.section as the array index(instead of indexPath.row). What you mentioned are not problems at all.
  • Chandramani
    Chandramani almost 8 years
    As "Abdullah Saeed" approch. you can try with setting header view too with all above step.
  • averydev
    averydev almost 8 years
    The default selection and editing modes do look bad, but those are easily overridden...
  • Mr G
    Mr G almost 8 years
    I had some remnants of the uiview for the header and footer on a non white background. easy fix. Just override the view for header and footer as well and set the view background color to clear.
  • iOS.Lover
    iOS.Lover over 7 years
    The result is too bad !
  • Walter White
    Walter White almost 7 years
    It's just not correct - it's a dirty way, because Sections and Cells are different concepts and when you use this way, you can't use Sections the way they should be used. This will lead to problems when your database grows, because you will not structure your data representation properly. averydev's answer is correct - use custom spacer cells.
  • damianostre
    damianostre almost 7 years
    This, like practically every other answer, does not work with dynamically sized cells where the height is calculated by their constraints: Adding a subview won't help unless you then also find all affected constraints and update them accordingly, which gets very complex, quickly.
  • averydev
    averydev almost 7 years
    @ThomasTempelmann I assume you have a good reason to size the cells based on constraints, but I've never found a great reason to do that. The closest i've come is to create a "template cell" based on constraints, populating it manually off screen, and then using the resulting size.
  • Erik Nguyen
    Erik Nguyen over 5 years
    Everyone please refer to this answer. It's so simple.