How to increase the UITableView separator height? - iphone
I want more space(10px) between each cell. How can I do this?
And I have added this code
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
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.
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.
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
In Swift
The easiest and shortest way for me was to add the snippet below in cellForRowAtIndexPath or in willDisplayCell:
override func tableView(tableView: UITableView,
willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
let additionalSeparatorThickness = CGFloat(3)
let additionalSeparator = UIView(frame: CGRectMake(0,
cell.frame.size.height - additionalSeparatorThickness,
cell.frame.size.width,
additionalSeparatorThickness))
additionalSeparator.backgroundColor = UIColor.redColor()
cell.addSubview(additionalSeparator)
}
this is quite old. Nevertheless I will post my approach.
Simply increase your cell height a bit and assign a mask layer to the cell, like that:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "...", for: indexPath)
// Configure the cell...
let maskLayer = CAShapeLayer()
let bounds = cell.bounds
maskLayer.path = UIBezierPath(roundedRect: CGRect(x: 2, y: 2, width: bounds.width-4, height: bounds.height-4), cornerRadius: 5).cgPath
cell.layer.mask = maskLayer
return cell
}
So in this example my seperator height will be 4.
Have fun!
You can do this entirely in the storyboard. Here is how:
go to the storyboard and select the tableview
Show the Size Inspector and from there set row height to say 140.
then show the Attributes Inspector and from there set your separator to Single Line and Style Plain and choose a color
then in the storyboard (or in Document Outline) select the cell
and again in the Size Inspector, under the Table View Cell, set custom Row Height to say 120.
That’s all. Your separator will be 20 units tall.
Kinda old thread, but since I only found hacky solutions in this thread,
here the solution that worked best for me (without additional UIView in every cell)
Swift 3:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//configure your cell...
cell.layer.shadowColor = UIColor.red.cgColor
cell.layer.shadowOffset = CGSize(width: 0, height: 1)
cell.layer.shadowOpacity = 1
cell.layer.shadowRadius = 0
cell.layer.masksToBounds = false
return cell
}
EDIT: Unfortunately this does not work if you scroll up in a table. I leave the answer here anyway, since it might be a solution if your table has limited content.
See Shadow on a UITableViewCell disappears when scrolling for more info.
For a table cell with height of 50 and a space of 5 pix between the rows. Width is 320.
Define the background of the cells to be clear:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor clearColor];
}
Set the height of the cells, this is the size of the row PLUS the delimiter:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 55;
}
And define in cellForRowAtIndexPath a box, with the size of the row (MINUS delimiter) to draw in the background color:
UILabel *headerBackgroundLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,50)];
backgroundBox.backgroundColor = [UIColor grayColor];
[cell addSubview:backgroundBox];
I do it a much simpler and more flexible way. Some may call it a hack. I call it pragmatic.
I hide the standard UITableViewSeparator. I then add a subview to my cell, using auto layout pin it to the top. Fix the height to what I desire. Pin it to the edges with a margin either side. Change it's background colour. I have a plain separator with the height i desire.
You may question how efficient this is having another UIView in the cell hierarchy. Is it really going to make a noticeable difference? Probably not - you've just taken the standard separator out of the table hierarchy anyway.
Swift 4
It's not possible to make the default separator higher. Instead you need to add a subview that will look as a separator to each cell (and optionally make the cell higher). You can do it for example in cellForRowAtIndexPath or in a UITableViewCell subclass.
In case you allow to select the cell, you need to add the subview for selected state as well, otherwise the separator would disappear when the cell is selected. That's why selectedBackgroundView is also configured.
Add this into your UITableViewController subclass:
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorStyle = .none
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.backgroundView = UIView(backgroundColor: .white)
cell.backgroundView?.addSeparator()
cell.selectedBackgroundView = UIView(backgroundColor: .blue)
cell.selectedBackgroundView?.addSeparator()
// configure the cell
return cell
}
Add this extensions into the same file at the bottom:
private extension UIView {
convenience init(backgroundColor: UIColor) {
self.init()
self.backgroundColor = backgroundColor
}
func addSeparator() {
let separatorHeight: CGFloat = 2
let frame = CGRect(x: 0, y: bounds.height - separatorHeight, width: bounds.width, height: separatorHeight)
let separator = UIView(frame: frame)
separator.backgroundColor = .gray
separator.autoresizingMask = [.flexibleTopMargin, .flexibleWidth]
addSubview(separator)
}
}
Here's an option that might work for some people
cell.layer.borderColor = UIColor.white.cgColor
cell.layer.borderWidth = 4.0
cell.layer.masksToBounds = true
The easier and safest solution to this problem is to turn off the table separator and use a UITableViewCell as a separator of variable height. Sure, you'll have to do some index math to figure out where items are, but really it's odd / even.
It won't break and you get the benefit of recyclable cells (no extraneous views to clean up).
First make tableview separator none from the storyboard. Then add UILabel/UIView at bottom of cell of height(you needed) using storyboard or Xib
For Swift 4
Implemented King-Wizard's solution to Swift 4:
public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let additionalSeparatorThickness = CGFloat(4)
let additionalSeparator = UIView(frame: CGRect(x: 0,
y: cell.frame.size.height - additionalSeparatorThickness, width: cell.frame.size.width, height: additionalSeparatorThickness))
additionalSeparator.backgroundColor = UIColor.groupTableViewBackground
cell.addSubview(additionalSeparator)
}
This is the easiest solution I've found:
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
" "
}
then just set the height to whatever you want:
tableView.sectionHeaderHeight = 30.0
I came across a way that has allowed me to effectively change the gap between cells.
In Interface builder I set the row height to be 46.
In the cellForRowAtIndexPath method of my TableView delegate I set the frame of the cell to be a smaller value.
cell.frame=CGRectMake(44,0,tableView.bounds.size.width,44)
This gives me a cell with a height of 44 that fits the width of the tableView but the space provided for the row will be 46 as defined in IB.
I was filling the cell programmatically anyway so this suited me fine.
You should implement
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
delegate method. and return 100.0 there.
Related
Is there a callback for ViewCell fully laid out in swift?
I have a UITableView with cells, the relevant part of the layout looks like this: everything is fine until i try to add shadows to that button(orange) and view ($-), this happens: The problem is that the view is not fully set in awakeFromNib, and it also not fully set during override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { So when can i add the shadow to those views without having to worry about the size not being set correctly yet? Aditional Info: if i set it during the cell setup, it does not work at first, but after scrolling it gets fixed for as long as the view is open. Edit 1: how do i add the shadows? I call a func for the cell that calls the next method passing the view as parameter, i think that #DonMag suggestion should work, subclassing should work, but is there no way to know when all the view is laid out in a TableView? static func addShadowToView(view: UIView, CornerRadius: CGFloat){ view.layer.cornerRadius = CornerRadius view.clipsToBounds = true view.layer.masksToBounds = false view.layer.shadowColor = UIColor.gray.cgColor view.layer.shadowOffset = CGSize(width: 0.1, height: 0.1) view.layer.shadowOpacity = 0.5 let shadowPath = UIBezierPath(roundedRect: view.bounds, cornerRadius: view.layer.cornerRadius) view.layer.shadowPath = shadowPath.cgPath }
swift tableview cell change width frame not work
This is my code but not work! func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyChatCell var frame = cell.frame if(indexPath%2 == 0){ frame.size.width = CGFloat(200) } else { frame.size.width = CGFloat(100) } cell.frame = frame return cell } I want change cell width to 100 or 200 per cell But frame.size.width not work
You can't change that, because all cells in a table view must have the same width, but can have different heights if needed. However, you can try to make the table view's .backgroundColor transparent, and maybe the content view of the cell, than add another "wrapper view" on the cell's content view and make it have different widths. It will create the "visual impression" that cells indeed have different widths.
UITableviewCell showing old data and new data on reload
I have a UITableViewCell and I am adding xib in cellForRowIndexPath method. It works fine until I update the model and call reloadData on UITableView. The cell is showing new data on top of the old data, I can see the label on the old label text. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) let customView:CustomView = UIView.fromNib() userView.frame = CGRect(x: 10, y: y, width: Int(self.tableView.bounds.size.width-50), height: 50) userView.label.text = data[indexPath.row] cell.addSubview(customView:) Any guesses why this could happen?
The quick answer is this: since cells are reused when dequeued, you are adding a new CustomView to a cell that already had a CustomView added when dequeued previously. One way you could handle this is to remove any existing CustomView from the hierarchy before creating a new one and adding it. To do this you could add a recognizable tag to the view each time, and then look for a view with that same tag to remove during your dequeue process, like this: //Remove existing view, if it exists if let existingView = cell.viewWithTag(999) { //A view was found - so remove it. existingView.removeFromSuperview() } let customView: CustomView = UIView.fromNib() //Set a tag so it can be removed in the future customView.tag = 999 customView.frame = CGRect(x: 10, y: y, width: Int(self.tableView.bounds.size.width-50), height: 50) customView.label.text = data[indexPath.row] cell.addSubview(customView) To me this feels like overkill, as it seems you should just add your customView to a custom UICollectionViewCell so you aren't essentially creating a custom cell on the fly, but that is just me. If you did this you could simply dequeue your custom cell and set the text on the label without having to add more views to the hierarchy all the time.
iOS8 Swift - Problems dynamically resizing tableview cells
Here's what my application looks like currently. You'll see that when the subtitle text label is only one line is resizes correctly, but when there's multiple lines it gets all messed up. I think this has to do with the constraints possibly? Right now I'm using the auto-layout constraints. You can see them in the screenshot. Here's the code that creates my cells. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> DealCell { let cell = tableView.dequeueReusableCellWithIdentifier("Deal Cell", forIndexPath: indexPath) as DealCell let currentBar = bars[indexPath.row] as BarAnnotation cell.barName.text = currentBar.name cell.deal.text = currentBar.deal cell.distanceToBar.text = String(format: "%.3f mi", currentBar.distance) // Set the height of the table view cells self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 44.0; return cell } Any ideas?
Use tableView:heightForRowAtIndexPath: function in the UITableViewDelegate. Using this you can individually set the height of each row separately. For more information : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath: Or you can use auto layout settings like in this article. http://useyourloaf.com/blog/2014/02/14/table-view-cells-with-varying-row-heights.html
How to give space between two cells in tableview?
I want a space between two cell in table view, I want cell like this, How can i do that?
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; }
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!
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..
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.
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.
If someone looking for Swift version. 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.userInteractionEnabled = false header.backgroundColor = UIColor.clearColor() return header } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 }
For people that looking for alternative way of showing gaps between cells without using sections, you may want to show alternate colours and height like below. Use clearColor for the 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; } }
Add these lines in 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)
Sometimes, you might really want to keep the tableview divided in rows, and have 1 section. For example, this could happen if you need to display a custom header for that table view that stays in place when you scroll through the section. What I would recommend doing in this case, is returning a bigger float than the normal height of a cell in: - (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath and then making sure that the table style is Plain, and that the cell separator is none. You can do that in the XIB file itself, or in code: self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.style = UITableViewStylePlain; Maybe also add the cell's 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 the cells, but at the same time keeping them as rows in one section (which sometimes is what you want).
I have used a simple and quick approach for this (using storyboard) Add UITableView in your controller Set the separator style to none (from attributes inspector) Set the height of the row 5 pts more from top and bottom from the desired height Now add an image in the cell, pin it from left and right but leave 5 pts space (for padding like feel) and set the background of the image same as the background you want for cell When the table view will be loaded, it will feel like there are spaces between cells.
For spacing between cells like the ones in your screenshot, there is no need for custom cells (for their look anyway, like the gradient bkg and so on, this could anyway be a good idea, but this won't help for your spacing between cells) To achieve this kind of spacing, simply use different sections in your UITableView. [EDIT] Everything is explained In Apple's TableView Programming Guide (and that's worth reading it, as it contains a lot of stuff you should know about tableviews)
The best way to do that using xib file. give top and bottom constraint to it. for example, here I give bottom constraint 10 and make sure give perfect height to cell as shown as given below.here in code 'joinTableViewCell' is fileName of xib file. extension JoinTableViewController: UITableViewDataSource,UITableViewDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 4 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = Bundle.main.loadNibNamed("joinTableViewCell", owner: self, options: nil)?.first as! joinTableViewCell cell.ImgView.layer.masksToBounds = true cell.ImgView.cornerRadius(cell.ImgView.bounds.width / 2) cell.lblName.text = "Christian Bell" cell.lblJoin.text = "Joined"+"\t"+"Sep 3" return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 90 } }
What you're trying to achieve, visually, would be the same as adding the content of each cell inside a container View with a gray background, and having that view inside the cell. I don't think there's a need to add spaces between cells.
I don't know why all the answers that complicated. KIS, only using storyboard I've put a UIView inside the tableCell Content View; now the UIView height is less than Content View height, and that's it! Play with the Content View color and the UIView color to get the desired result.
My easy 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
* WORKING WITH IOS 9 XCODE 7.3 * The most straight forward way to achieve this is to simply add this code to your 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 board and click on the tableview. Go to identity inspector and change the View's background color to whatever border color was set in the method. Voila! Play with the values to get the desired output. Hope this helps! Note: If using Chameleon library you must set the background color for the view in code, not via the story board plugin. For some reason the color seems to be off by a shade.
Well what I did is simply simulate the space by creating a simple custom cell that is a little larger than what I want (cell + gap/2), and then put all my cell's content in an inner-view. Then put your cell's topmost view as the color of your background, and the inner-view as your actual cell. And you'll have the illusion of having space between cells, when it's actually just a larger cell with borders.
In Table View DataSource there are two methods named number of sections and number of rows In sections return 3; In Rows return 1;
You don't have to assign each section for each cell. Just create a UIView (container) inside your cell, margin it with cell's view. And we layout components like label, text, image on that container.
-(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; }
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 Color Code RGB values from:
1) first create 2 sections in table view. 2) create an empty cell. 3) create the cell with 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; } } else if (indexPath.row % 2 != 1) { return 100.0; } else { return 15.0; } } It will add space between the cells. It worked for me.
Here is my method with Swift 3: In ViewDidLoad(), add: self.tableView.rowHeight = 500.0 In tableview "CellForRowAt", add 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 }
in your cellForRowAt cell.layer.borderWidth = 2 cell.layer.cornerRadius = 10 cell.layer.borderColor = UIColor.systemBackground.cgColor
I suggest to create a custom UITableViewCell base class and use this class like below, Create custom UITableViewCell class Create a UIView in new class, this will act as the 'baseContentView' and it will be immediate child of 'UITableViewCell.contentView' Adjust the top 'padding' on 'baseContentView' (this will be the separator/gap) from parent view (UITableViewCell.contentView) Inherit all your custom classes from this class rather than UITableViewCell Add all the content/subviews to 'baseContentView' rather than 'self.contentView' You can play with the 'padding' as per your need.
I have 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 You can create a subview to show the content of cell and the rest is the gap as you wish.