Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 设置UITableViewCell分组的选择样式背景色_Objective C_Cocoa Touch_Uitableview - Fatal编程技术网

Objective c 设置UITableViewCell分组的选择样式背景色

Objective c 设置UITableViewCell分组的选择样式背景色,objective-c,cocoa-touch,uitableview,Objective C,Cocoa Touch,Uitableview,我想设置UITableViewCell在分组表视图中高亮显示时的背景色。我现在使用的代码产生了这样的结果。有人能告诉我这样做的正确方法吗,以便保持uitableviewcell的圆角 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { cell.selectedBackgroundView = [[UIView alloc] initWit

我想设置UITableViewCell在分组表视图中高亮显示时的背景色。我现在使用的代码产生了这样的结果。有人能告诉我这样做的正确方法吗,以便保持uitableviewcell的圆角

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

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;

更新:

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;

尝试确保单元格的clipsToBounds设置为YES


通过使用Bazier路径,您可以给出顶部两个或底部两个或任意数量的拐角半径(显然是4个)

这里我设置了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
现在,您只需要设置第一个和最后一个单元格背景的角

记住在项目中添加QuartzCore框架,并在类中导入

突出显示时,您可以使用以下命令更改textlable的textcolor:

cell.textLabel.highlightedTextColor = [UIColor blackColor];

另一个选择是在背景视图单元格中添加半径。selectedBackgroundView.layer.cornerRadius=5.0f;最后一个选项是创建您自己的单元格选项2,cell.selectedBackgroundView.layer.cornerRadius几乎就在这里,只是它将顶部单元格的底部两个角和顶部单元格的底部两个角都圆角,这两个角都是不需要的。你有解决方法吗?在这种情况下,你将不得不做一个自定义单元不幸的是,如果你按照上面的教程,它应该不会太麻烦你这么近!见上面编辑的问题。不知道为什么右上角不是圆的…因为你的单元格宽度很大。检查你的手机大小你是个天才。感谢这个解决方案我很高兴,不是天才只是一个幸运的猜测:)这个解决方案确实很棒:)还有一件事,我如何将文本保持在UITableviewCell中的黑色?此时,当单元格高亮显示(即按下但未选中)时,它将变为白色