为UITableViewCell分组设置选择样式BackgroundColor

Apo*_*llo 7 cocoa-touch objective-c uitableview

我想在分组的tableview中突出显示UITableViewCell的背景颜色.我现在使用的代码产生了这个结果.有人可以告诉我这样做的正确方法,以便保持uitableviewcell的圆角吗?

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

更新的代码:

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;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

更新:

在此输入图像描述

KDe*_*kar 7

通过使用Bazier路径,您可以给出前两个或后两个或任意数量的角半径(明显为4个).

在这里,我设置了单元格背景视图的左上角和右上角:

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
Run Code Online (Sandbox Code Playgroud)

现在你只想设置第一个和最后一个单元格背景的角落.

并记住在您的项目和#import <QuartzCore/QuartzCore.h>类中添加QuartzCore Framework .

使用以下方法突出显示时,可以更改textlable的textcolor:

cell.textLabel.highlightedTextColor = [UIColor blackColor];
Run Code Online (Sandbox Code Playgroud)