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;
Update:
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
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.
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.
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With