3

I want to remove the cell separators in my UITableView. This is how my simulator currently looks:

enter image description here

I have implemented the following line of code in the viewDidLoad: method of the .swift file of the view controller:

self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None

This doesn't seem to be working though.

How do I remove the cell separators?

Here is my cellForRowAtIndexPath: code:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! RestaurantsTableViewCell

        // Configure the cell...
        cell.nameLabel.text = restaurants[indexPath.row].name
        cell.typeLabel.text = restaurants[indexPath.row].type
        cell.mainImageView.image = UIImage(named: restaurants[indexPath.row].image)

    return cell

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • That row should be enough, I think that you might have some padding between your rows. Post some code for your tableView. – Rashwan L Jan 04 '16 at 23:55
  • @RashwanL What code do you want to see? –  Jan 04 '16 at 23:56
  • Begin with with the function `cellForRowAtIndexPath`. – Rashwan L Jan 04 '16 at 23:57
  • @RashwanL I have added this code. –  Jan 04 '16 at 23:58
  • Are you sure that your image does not have a border? – Rashwan L Jan 04 '16 at 23:59
  • @RashwanL How do I check this? It does have `clipsToBounds = true` and `Clip Subviews` enabled. –  Jan 05 '16 at 00:00
  • Open the image and check :) – Rashwan L Jan 05 '16 at 00:00
  • @RashwanL What must I see for it to have a border? –  Jan 05 '16 at 00:01
  • Upload these three images that you have in your tableView to your question and I´ll take a look. – Rashwan L Jan 05 '16 at 00:02
  • @RashwanL I didn't understand the question at first. But no, they don't have a border. –  Jan 05 '16 at 00:02
  • If UIImageView's layout constraints are okay, I'd say it is cell height a bit larger than image height, so there's some visual space between image and cell bounds. Try correcting your cell height, or set UIImageView to scale image instead of centering it. – Alex Skalozub Jan 05 '16 at 00:15
  • @AnnabelleSykes, I have added an example for you. – Rashwan L Jan 05 '16 at 00:15
  • @AlexSkalozub This removed the cell separator, but when I swipe back and forth, the cell height changes. –  Jan 05 '16 at 00:33
  • @AnnabelleSykes you might want to implement `tableView:heightForRowAtIndexPath:` to control cell height – Alex Skalozub Jan 05 '16 at 00:44
  • @AlexSkalozub Could you show me the code to write? –  Jan 05 '16 at 00:46
  • Something like `override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 100; /* or whatever your height is */ }`. If there's still something wrong with it, check UIImageView constraints again. – Alex Skalozub Jan 05 '16 at 00:51
  • @AlexSkalozub This didn't change anything. –  Jan 05 '16 at 00:55
  • @AnnabelleSykes you have some error in IB layout then. Can't tell which without seeing it. – Alex Skalozub Jan 05 '16 at 01:03
  • @AlexSkalozub I fixed it with some help! It was because I hadn't set the layout constraints for the cell. Thanks for your help though! –  Jan 05 '16 at 01:04

3 Answers3

2
  1. Check your layout of cell,especially top and bottom constraints.enter image description here
  2. Check the content view of cell.Did you set the clips to bounds to true?enter image description here enter image description here

All the above should be checked both in code and IB.

Lumialxk
  • 6,239
  • 6
  • 24
  • 47
0

To hide the separator line you should set the separator insets to zero for each cell. You should add this line in your cellForRowAtIndexPath:

cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
adrianokw
  • 387
  • 3
  • 10
  • This didn't change anything. Thank you though! –  Jan 05 '16 at 00:23
  • 1
    Instead of setting the insets to zero, you should set it with the "right" inset to the width of the table view, i.e., `cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: tableView.bounds.width)` – utogaria Sep 14 '16 at 07:01
0

I suggest giving the cells a random background color instead of white so you can make sure the cell has padding and it's not part of image. If it didn't work you may try setting the inset to zero using both the table and cell reference. Look at this answer for more explanation.

Remove SeparatorInset on iOS 8 UITableView for XCode 6 iPhone Simulator

Community
  • 1
  • 1
MKoosej
  • 3,405
  • 3
  • 21
  • 29