开发者

Removing cell borders from a section of grouped-style UITableView

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, bu开发者_JAVA技巧t 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!


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];


You have to actually set

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

to remove the border of cells.


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];
}


This is what worked for with having a Grouped style table

[tableView setSeparatorColor:[UIColor clearColor]];


This code worked for me :)

[self.tableView setSeparatorColor:[UIColor clearColor]];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];


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


cell.backgroundColor = [UIColor clearColor];

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


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


cell.backgroundView = [UIView new];

Works like a charm! Tested! iOS6


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

Removing cell borders from a section of grouped-style UITableView


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


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

[tableViewOutlet setBackgroundView:nil];

in the viewDidLoad method.


 UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
 backView.backgroundColor = [UIColor clearColor];
 cell.backgroundView = backView;
 cell.backgroundColor = [UIColor clearColor];
 [cell.contentView addSubview:imageView];


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];
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜