64

I have a UITableViewController initialized with the grouped style and having multiple sections. For one of these sections, I'd like its constituent cells to be completely transparent and have no border. I plan to assign a custom view for every row in this section, but having that custom view surrounded by the grouped table cell looks bad :(

The following makes the background color of a cell black instead of transparent... And I still don't know how to get rid of the border.

cell.backgroundColor = [UIColor clearColor];

Any pointers? Thanks!

Tim
  • 641
  • 1
  • 6
  • 3

15 Answers15

159

NOTE: This doesn't appear to be working in iOS7 and above. For iOS7 try this answer.

For iOS6 and below, to remove the grouped background from a cell in a grouped table view cell:

This didn't work

cell.backgroundView = nil; // Did Not Work

This did

cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];

If you have moved to ARC (I've heard this works, but haven't tested it)

cell.backgroundView = [UIView new];
Community
  • 1
  • 1
user160917
  • 9,211
  • 4
  • 53
  • 63
  • 1
    This works for me for iOS 4.3.3 too and i prefer this solution, because you can remove the border for each cell individually instead of removing it for all cells of the table view. – anka Jul 26 '11 at 09:18
  • Also set the cell.selectedBackgroundView using the same otherwise it may highlight. – nh32rg Jul 07 '12 at 23:03
  • [cell setBackgroundColor:t.best5bgColor]; – Prince Kumar Sharma May 16 '13 at 07:23
  • 2
    @RyanRomanchuk I've found a way that works in iOS 7: http://stackoverflow.com/a/19220030/1372503 – Andreas Ley Oct 07 '13 at 08:23
  • Thanks a lot buddy, wish I found this earlier. For iOS 7, I think it doesn't add any style for grouped tables. – Piyuesh Oct 18 '13 at 06:58
  • @Intentss Brilliant stuff. This works whereas the solution provided below by Fede Mika ie [tableView setSeparatorColor:[UIColor clearColor]]; does NOT work on iOS6.1. Actually I am using ARC, and your second solution, slightly modified, worked cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero]; Just to let people know that I have built a universal app that works on iOS6.1 - iOS10+. So, a lot of these little solutions are very useful for cross compatibility. I was unable to test on iOS6 & iOS7, using XCode 8, so I just created Ad Hoc '.ipa' files for this. – Charles Robertson Dec 27 '16 at 17:51
39

You have to actually set

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

to remove the border of cells.

Chintan Patel
  • 3,175
  • 3
  • 30
  • 36
  • 17
    The question is asking about grouped tables. `tableView.separatorStyle = UITableViewCellSeparatorStyleNone` doesn't work on grouped tables. – Steven Fisher Jul 26 '12 at 06:30
37

The following hack works in iOS 7 – for now. :)

Subclass UITableViewCell, and use this cell for the section that shouldn't have separators.
Override the addSubview method in your cell subclass:

-(void)addSubview:(UIView *)view
{
    // The separator has a height of 0.5pt on a retina display and 1pt on non-retina.
    // Prevent subviews with this height from being added. 
    if (CGRectGetHeight(view.frame)*[UIScreen mainScreen].scale == 1)
    {
        return;
    }

    [super addSubview:view];
}
Andreas Ley
  • 9,109
  • 1
  • 47
  • 57
23

This is what worked for with having a Grouped style table

[tableView setSeparatorColor:[UIColor clearColor]];

Fede Mika
  • 2,211
  • 2
  • 19
  • 18
  • This solution worked for me! And I think this is a better solution than "cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];" from performance standpoint. I am using Grouped style table, iOS 5.1. Thanks – Rohit Gupta Sep 04 '12 at 08:15
  • 2
    I don't know what kind of performance you are talking about, generally setting anything to [UIColor clearColor] is bad idea when it comes to graphics performance because it's a non-opqaue layer that forces the GPU to do transparency blending. – user160917 Sep 08 '12 at 00:22
  • 1
    if I use the above with a grouped tableview, I get that the overall border of the table is gone, which is what I expected. It seems that separator color == table border. – abellina Jan 04 '13 at 22:58
  • The other nice thing about this answer is that you can use it in IB too, so you can see your cell as it is meant to look. – siburb Jul 20 '13 at 01:07
  • But this removes the separator from the whole table view, not from a single section. – Pin Oct 11 '13 at 10:08
19

This code worked for me :)

[self.tableView setSeparatorColor:[UIColor clearColor]];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
Nicolò Ciraci
  • 678
  • 5
  • 23
2

Set the backgroundView of the cell to nil. For a grouped table, the cell image is part of that view.

user511132
  • 199
  • 1
  • 1
  • 4
1

Try using tableView.separatorColor = [UIColor clearColor];

And, don't use tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

I tested with both, if style is none, making the section borders invisible is not working, but instead just change its color, and section border will appear to be none.

iOS seems to be differentiating making an object none and making an object transparent

Titouan de Bailleul
  • 12,920
  • 11
  • 66
  • 121
petershine
  • 3,190
  • 1
  • 25
  • 49
1
cell.backgroundColor = [UIColor clearColor];

cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
Titouan de Bailleul
  • 12,920
  • 11
  • 66
  • 121
Sandip Patel - SM
  • 3,346
  • 29
  • 27
1
cell.backgroundView = [UIView new];

Works like a charm! Tested! iOS6

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
1

As of iOS 8, setting the separator attribute to none works as well.

Get rid of cell border

prabhu
  • 878
  • 9
  • 17
0

Setting a content view also gets rid of the border. Set your custom view to cell.contentView.

BTRUE
  • 390
  • 3
  • 8
0

The easiest way to remove cell borders from a section of grouped-style UITableView:

[tableViewOutlet setBackgroundView:nil];

in the viewDidLoad method.

Kevin Zych
  • 751
  • 13
  • 21
0
 UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
 backView.backgroundColor = [UIColor clearColor];
 cell.backgroundView = backView;
 cell.backgroundColor = [UIColor clearColor];
 [cell.contentView addSubview:imageView];
0

If you have a custom UITableCellView then you can add the following method to your view to remove the background view.

- (void)setBackgroundView:(UIView *)backgroundView
{
    // We don't want background views for this cell.
    [super setBackgroundView:nil];
}
Kostub Deshmukh
  • 2,852
  • 2
  • 25
  • 35
0

I just thought I would convert my comment to @Intentss into an answer, because it maybe useful for those, using his solution.

Using iOS6.1 with a grouped UITabelView, using ARC:

[tableView setSeparatorColor:[UIColor clearColor]];

Does not work

cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];

Does work

Charles Robertson
  • 1,760
  • 16
  • 21