【问题标题】:Setting Selection Style BackgroundColor for UITableViewCell Grouped为分组的 UITableViewCell 设置选择样式 BackgroundColor
【发布时间】:2013-07-20 02:48:02
【问题描述】:

我想设置 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;

更新:

【问题讨论】:

    标签: objective-c cocoa-touch uitableview


    【解决方案1】:

    通过使用 Bazier Path,您可以给出顶部两个或底部两个或任意数量的拐角半径(显然是 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
    

    现在您只想为第一个和最后一个单元格背景设置角。

    记得在你的项目中添加 QuartzCore 框架,在你的类中添加 #import <QuartzCore/QuartzCore.h>

    您可以在突出显示时更改 textlable 的文本颜色:

    cell.textLabel.highlightedTextColor = [UIColor blackColor];
    

    【讨论】:

    • 离你这么近!请参阅上面编辑过的问题。不知道为什么右上角不圆...
    • 因为您的单元格宽度很大。检查您的单元格大小
    • 你是个天才。感谢您的解决方案
    • 我的荣幸,而不是天才只是一个幸运的猜测:)
    • 嗯,这个解决方案确实很棒:) 还有一件事,我如何让 UITableviewCell 中的文本保持黑色?现在,当单元格突出显示(即按下但未选中)时,它变为白色
    【解决方案2】:

    【讨论】:

    • 另外一个选择是给你的背景视图添加一个半径 cell.selectedBackgroundView.layer.cornerRadius = 5.0f;
    • 最后一个选项是制作自己的单元格app-solut.com/blog/2011/04/…
    • 选项 2,cell.selectedBackgroundView.layer.cornerRadius 几乎就在那里,除了它将顶部单元格的底部两个角和顶部单元格的底部两个角都四舍五入,两者都是不想要的。你有解决方法吗?
    • 在这种情况下,不幸的是,您将不得不制作一个自定义单元格,但是如果您按照上面的教程进行操作,应该不会有太大的麻烦
    猜你喜欢
    • 2011-07-10
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    相关资源
    最近更新 更多