Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Selection Style BackgroundColor for UITableViewCell Grouped

I want to set the background color of a UITableViewCell when it's highlighted in a grouped tableview. The code I'm using now produces this results. Could somebody please inform me of the correct way to do this, so as to maintain the rounded corners of the uitableviewcell?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds] ;
    cell.selectedBackgroundView.backgroundColor = coolBlue ;
}

Updated Code:

cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
cell.selectedBackgroundView.backgroundColor = coolBlue;

UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:cell.selectedBackgroundView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(8.0f, 8.0f)];

CAShapeLayer *shape = [[CAShapeLayer alloc] init];

[shape setPath:rounded.CGPath];

cell.selectedBackgroundView.layer.mask = shape;
rounded = nil;
shape = nil;

enter image description here

Update:

enter image description here

like image 664
Apollo Avatar asked Jul 20 '13 02:07

Apollo


People also ask

How to change the background color of a cell in Tableview?

Table View Cell selection background color can be set via the Storyboard in Interface Builder: If you have a grouped table with just one cell per section, just add this extra line to the code: bgColorView.layer.cornerRadius = 10; Don't forget to import QuartzCore. Swift 3: for me it worked when you put it in the cellForRowAtIndexPath: method

How do I set the background color of selected cells?

All you have to do is set the background color in the touch events, which simulates selection on touch, and then set the background color in the setSelected function. Setting the background color in the selSelected function allows for deselecting the cell.

How to change the background color of a cell in UIView?

The default is nil for cells in plain-style tables (UITableViewStylePlain) and non-nil for section-group tables UITableViewStyleGrouped). Therefore, if you're using a plain-style table, then you'll need to alloc-init a new UIView having your desired background colour and then assign it to selectedBackgroundView.

How to deselect a cell in uitableviewcell?

I have a subclassed UITableViewCell. All you have to do is set the background color in the touch events, which simulates selection on touch, and then set the background color in the setSelected function. Setting the background color in the selSelected function allows for deselecting the cell.


1 Answers

By Using Bazier Path you can able to give top two or bottom two or any numbers of corner radius(out of 4 obviously).

Here I am setting top left and top right corner for cell backgroundview:

UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:cell.selectedBackgroundView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(8.0f, 8.0f)];

CAShapeLayer *shape = [[CAShapeLayer alloc] init];

[shape setPath:rounded.CGPath];

cell.selectedBackgroundView.layer.mask = shape;
rounded = nil;
shape = nil;  
// you can change the code as per your need

And now you just want to set the corner for first and last cell background.

And Remember to add QuartzCore Framework in your project and #import <QuartzCore/QuartzCore.h> in your class.

you can change the textcolor of textlable when highlighted using:

cell.textLabel.highlightedTextColor = [UIColor blackColor];
like image 189
KDeogharkar Avatar answered Sep 27 '22 22:09

KDeogharkar