Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase the UITableView separator height?

I want more space(10px) between each cell. How can I do this?

And I have added this code

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
like image 412
vinay Avatar asked Aug 19 '10 11:08

vinay


People also ask

How to add separator in tableView swift?

Behaviour 1: Add section header to only the third section. What did I try: Add a one pixel height, tableView width UIView, with all required properties set(color, etc) and add it as a sub-view to the section header view's. Add drop shadow with CGPoint(0,1) to the section header views.


3 Answers

The best way for me, just add this in cellForRowAtIndexPath or in willDisplayCell

CGRect sizeRect = [UIScreen mainScreen].applicationFrame;    
NSInteger separatorHeight = 3;
UIView * additionalSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,cell.frame.size.height-separatorHeight,sizeRect.size.width,separatorHeight)];
additionalSeparator.backgroundColor = [UIColor grayColor];
[cell addSubview:additionalSeparator];

For Swift 3.0:

let screenSize = UIScreen.main.bounds
let separatorHeight = CGFloat(3.0)
let additionalSeparator = UIView.init(frame: CGRect(x: 0, y: self.frame.size.height-separatorHeight, width: screenSize.width, height: separatorHeight))
additionalSeparator.backgroundColor = UIColor.gray
self.addSubview(additionalSeparator)

You should add this to cell's method awakeFromNib() to avoid re-creation.

like image 196
Dmitry Soloviov Avatar answered Oct 22 '22 08:10

Dmitry Soloviov


I have seen many clunky solutions like subclassing UITableView with hidden cells, and other less optimal ones incl. in this thread.

When initializing the UITableView, Set the rowHeight property of UITableView to a height that equals = cell height + desired separator/space height.

Do not use standard UITableViewCell class though, instead, subclass the UITableViewCell class and override its layoutSubviews method. There, after calling super (don't forget that), set the height of the cell itself to desired height.

BONUS UPDATE 18/OCT/2015:

You can be a bit smart about this. The solution above basically puts the "separator" at the bottom of the cell. What really happens is, the row height is managed by the TableViewController but the cell is resized to be a bit lower. This results in the separator/empty space being at the bottom. But you can also centre all the subviews vertically so that you leave the same space at the top and the bottom. For example 1pt and 1pt.

You can also create isFirst, isLast convenience properties on your cell subclass. You would set these to yes in the cellForRowAtIndexPath. This way you can handle the edge cases for top and bottom separators inside the layoutSubviews method as this would have access to these properties. This way you can handle the edge cases for top or bottom - because sometimes the design department wants N+1 separators while the number of cells is only N. So you have to either deal with the top one or the boot one in a special way. But it's best do this inside cells instead tableViewHeader or TableViewFooter.

like image 21
Earl Grey Avatar answered Oct 22 '22 08:10

Earl Grey


I don't think it's possible using standard API. I guess you would need to subclass the UITableViewCell and add a view that simulates a separator at the bottom of the cell.

You may want to check this question, it seems related and has some sample code: iPhone + UITableView + place an image for separator

like image 14
Mayjak Avatar answered Oct 22 '22 10:10

Mayjak