How to provide space between two cells in a table?

I need the space between two cells in a table view,

I want a cell like that

enter image description here

How can i do this?

+44
objective-c iphone cocoa-touch uitableview ios4
Aug 25 '11 at 11:23
source share
24 answers

You can create TableView partitions also in UITableView ... These methods are required, so create partitions, and in each section you can create one cell, as in the picture.

+20
Aug 25 '11 at 11:28
source share

you cannot set the distance between cells directly, but you can set the height for the title in the section to achieve the same result.

1. Enter the cell numbers you need in the form of 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 of the header in the section to set the space between cells

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

4. set the background color of the title to clear the 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; } 
+118
Feb 06 '13 at 10:14
source share

The best way to get the space between two cells in a TableView is to declare the partition numbers you want in the numberofsections delegation method this way

For example, you have an 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 in the delegate method cellForRowAtIndexPath you need to use indexpath.section, but not indexpath.row

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

This is checking your table space for the space between two cells. Enjoy it!

+25
Oct 24
source share

The answer to several sections will work, but it is very fragile and does not allow sections relevant . Instead, you should create a custom cell or your own cell prototype that just has a space at the bottom and / or top.

Use your struts and springs in IB to maintain this uniform clearance and use heightForRowAtIndexPath to return a height that includes a space.

+16
Feb 19 '14 at 21:27
source share

Goal - 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

 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.

+11
Feb 11 '15 at 5:20
source share

If someone is looking for a version of Swift . Here you go.

 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 10; // space b/w cells } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return items.count // count of items } func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let header = UIView() header.backgroundColor = UIColor.clearColor() return header } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } 
+10
Jan 23 '15 at 20:47
source share

For people who are looking for an alternative way to display spaces between cells without using sections, you can display alternative colors and heights as shown below. Use clearColor for spacing.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { tableView.separatorStyle = UITableViewCellSeparatorStyleNone; if (indexPath.row % 2 == 1) { static NSString *CellIdentifier = @"cellID1"; UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.backgroundColor = [UIColor colorWhite]; return cell; } else { static NSString *CellIdentifier2 = @"cellID2"; UITableViewCell *cell2 = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; if (cell2 == nil) { cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]; } cell2.backgroundColor = [UIColor clearColor]; return cell2; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row % 2 == 1) { return 40.0; } else { return 2.0; } } 
+6
Dec 05 '13 at 3:19
source share

Add these lines to the cellForRowAtIndexPath UITableViewDelegate method before returning the cell.

 let separator = UIView(frame: CGRectMake(0, 0, cell!.bounds.size.width, 1)) separator.backgroundColor = UIColor.whiteColor() cell.contentView.addSubview(separator) 
+5
Apr 12 '16 at 14:19
source share

Sometimes you really need to leave the table view divided in rows and have 1 section. For example, this can happen if you need to display a custom title for this table view, which remains in place while scrolling through a section.

What I would recommend doing in this case returns a higher float than the normal cell height in:

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

and then make sure the table style is Plain and the cell separator is not. You can do this in the XIB file itself or in the code:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.style = UITableViewStylePlain;

Perhaps also add a cell selection style to none (otherwise it will look like you are selecting more than just the visible part of the cell).

cell.selectionStyle = UITableViewCellSelectionStyleNone;

This will give the impression of space between cells, but at the same time keeps them as rows in one section (which sometimes is what you want).

+4
Apr 30 '13 at 16:09
source share

I used a simple and quick approach for this (using the storyboard)

  • Add UITableView in controller
  • Set the separator style to none (from the attribute inspector)
  • Set the line height to 5 points more above and below from the desired height
  • Now add the image to the cell, attach it to the left and right, but leave 5 points (to fill, for example, the sensation) and set the background of the image to the same as the background you want for the cell

When the table is loaded, there will be a feeling that there are spaces between the cells.

+4
Aug 06 '15 at 15:57
source share

For the spacing between cells similar to the ones in your screenshot, there is no need for custom cells (for viewing them anyway, for example, the bkg gradient, etc., this may be a good idea, but this will not help the distance between the cells )

To achieve this kind of spacing, just use different sections in your UITableView.

[EDIT] Everything is explained in the Apple TableView Programming Guide (and it is worth reading because it contains a lot of things you need to know about tables)

+3
Aug 25 '11 at 11:29
source share

What you are trying to achieve visually will be the same as adding the contents of each cell inside the View container with a gray background and having this view inside the cell. I do not think there is a need to add spaces between cells.

+2
Aug 07 '14 at 20:00
source share

* WORKING WITH IOS 9 XCODE 7.3 *

The easiest way to achieve this is to simply add this code to the cellForRowAtIndexPath method.

  cell.separatorInset.left = 20.0 cell.separatorInset.right = 20.0 cell.separatorInset.top = 20.0 cell.separatorInset.bottom = 20.0 cell.layer.borderWidth = 3 cell.layer.cornerRadius = 20.0 cell.layer.borderColor = UIColor.flatSkyBlueColorDark().CGColor 

Then go to your story panel and click on the table. Go to the identity inspector and change the Background color of the View to any border color set in the method. Voila! Play with the values ​​to get the desired result. Hope this helps!

Note. If you use the Chameleon library, you should set the background color for presentation in the code, and not through the story board plugin. For some reason, the color seems to be shaded.

+2
May 17 '16 at 21:24
source share

Well, what I did is just simulate the space, creating a simple custom cell that is a little bigger than I want (cell + gap / 2), and then putting the entire contents of my cell in the internal view.

Then put the top view of your camera as the background color and the inside view as your actual cell. And you will have the illusion of the presence of space between cells, when in fact it is just a large cell with borders.

+1
Jan 27 '15 at 10:10
source share

I do not know why all the answers are complicated. KIS, only using the storyboard I put the UIView inside the TableCell Content View; now the height of the UIView is less than the height of the content, and what a thing!

Play with the Content View color and the UIView color to get the desired result.

+1
Jan 13 '16 at 9:45
source share

My simple solution using Swift :

 // Inside UITableViewCell subclass override func layoutSubviews() { let f = contentView.frame let fr = UIEdgeInsetsInsetRect(f, UIEdgeInsetsMake(10, 10, 10, 10)) contentView.frame = fr } 

or one line code

 override func layoutSubviews() { contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, UIEdgeInsetsMake(10, 10, 10, 10)) } 

Result enter image description here

+1
Mar 17 '16 at 12:35
source share

In Table View DataSource, there are two methods named the number of partitions and the number of rows In partitions return 3; In lines return 1;

0
Aug 25 2018-11-11T00:
source share

You do not need to assign each section to each cell. Just create a UIView (container) inside your cell, put it in the cell. And we compose components such as label, text, image in this container.

0
Apr 16 '16 at 10:52
source share

I suggest creating your own base class UITableViewCell and using this class as shown below,

  • Create custom class UITableViewCell
  • Create a UIView in a new class, it will act as a "baseContentView" and it will immediately be a child of "UITableViewCell.contentView"
  • Adjust the top “registration” to “baseContentView” (it will be a separator / space) from the parent view (UITableViewCell.contentView)
  • Inherit all your custom classes from this class, not UITableViewCell
  • Add all contents / routines to 'baseContentView' and not to self.contentView

You can play with "fill" according to your needs.

0
Aug 20 '16 at 21:40
source share
 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return __titlesArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 10; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *header = [[UIView alloc]init]; header.backgroundColor = [UIColor clearColor]; return header; } 
0
Aug 25 '16 at 8:49
source share

I use like:

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 194.0; } override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { cell.contentView.backgroundColor = UIColor.clearColor() let whiteRoundedView : UIView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, 185)) whiteRoundedView.backgroundColor = UIColor( red: CGFloat(61.0/255.0), green: CGFloat(117.0/255.0), blue: CGFloat(147.0/255.0), alpha: CGFloat(1.0)) whiteRoundedView.layer.masksToBounds = false whiteRoundedView.layer.cornerRadius = 3.0 whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, 1) whiteRoundedView.layer.shadowOpacity = 0.5 cell.contentView.addSubview(whiteRoundedView) cell.contentView.sendSubviewToBack(whiteRoundedView) } 

Get RGB color code values:

enter image description here

0
Jan 17 '17 at 8:45
source share

1) first create 2 sections in the form of a table. 2) create an empty cell. 3) create a cell with the data you want to display. 4) use the method

  • (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath {if (indexPath.section == 0) {

    if (indexPath.row% 2! = 1) {return 15.0; } else {return 100; } } yet

     if (indexPath.row % 2 != 1) { return 100.0; } else { return 15.0; } 

}

It will add space between cells. It worked for me.

0
Jul 07 '17 at 10:39 on
source share

Here is my method with Swift 3:

In ViewDidLoad () add:

 self.tableView.rowHeight = 500.0 

In the CellForRowAt table view, add the following:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) // Configure the cell... var actualContentView = UIView() actualContentView.frame = CGRect(x: 10, y: 10, width: cell.contentView.frame.width - 20, height: cell.contentView.frame.height - 20) actualContentView.backgroundColor = UIColor.blue cell.contentView.addSubview(actualContentView) return cell } 
0
Jul 08 '17 at 15:48
source share

I checked all the answers, but I think this is the easiest way:

  UIView * theContentView = [UIView alloc]initWithFrame:CGRectMake(0,gap,width,height)]; theContentView.backgroundcolor = [UIColor lightGrayColor];//contentColor cell.backgroundcolor = [UIColor blackColor];//gapColor [cell addSubview: theContentView] 

The prototype code says that you can create a subitem to display the contents of the cell, and the rest is a space of your choice.

-one
08 Oct '15 at 2:57
source share



All Articles