更改iOS6中UITableView分组样式的圆角半径。

8

我尝试过使用以下代码,但都没有成功。有人知道在iOS 6中如何做到这一点吗?我不想创建自定义单元格。

self.tableView.layer.cornerRadius = 5.0f;
    [self.tableView setClipsToBounds:YES];

编辑:

看起来实际上这段代码是为整个视图创建圆角,而不是为每个单独的UITableViewSection创建圆角。这有意义吗?

我还尝试过 [cell.layer setCornerRadius:3.0]; 但仍然没有成功。我的UITableView的角仍然完全相同。

enter image description here


我可以问一下为什么您不想要自定义单元格吗?它会让生活变得更加轻松。 - Rob van der Veer
@RobvanderVeer 我过去使用自定义单元格时遇到了困难,但我想我会考虑使用自定义单元格的建议。我主要担心性能问题。有人建议为整个单元格使用自定义图像,这似乎是解决这个问题的一种懒惰和不正确的方式。 - Apollo
我建议打开一个游乐场项目进行实验,这样你就可以做出更好的决定。 - Rob van der Veer
你是想将整个TableView的边角进行圆角处理,还是只想对其中一部分或某个“section”进行处理?能否澄清一下? - Mick MacCallum
为什么你不使用带有图片的自定义单元格? - 3lvis
你展示的屏幕截图是 iOS 7 的,你真的想在 iOS 6 或 7 中使用它吗? - Prince Kumar Sharma
10个回答

19
您可以更改TableViewCell的backgroundView,创建一个UIView的子类并更改其layer class:
您可以更改TableViewCell的backgroundView,创建一个UIView的子类并更改其layer class:
@interface BackgroundView : UIView
@end

@implementation BackgroundView

+ (Class)layerClass
{
    return [CAShapeLayer class];
}
@end

稍后在cellForRowAtIndexPath中,您会执行以下操作:

static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

CGRect frame = cell.backgroundView.frame;
cell.backgroundView = [[BackgroundView alloc] initWithFrame:frame];

CGFloat corner = 20.0f;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:cell.backgroundView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(corner, corner)];
CAShapeLayer  *shapeLayer = (CAShapeLayer *)cell.backgroundView.layer;
shapeLayer.path = path.CGPath;
shapeLayer.fillColor = cell.textLabel.backgroundColor.CGColor;
shapeLayer.strokeColor = [UIColor lightGrayColor].CGColor;
shapeLayer.lineWidth = 1.0f;

return cell;

结果如下:

在此输入图片描述

您可以修改所需的角落或创建另一条路径。

希望对您有所帮助。


1
我不确定为什么这个答案被接受了。如果我错了,请纠正我,OP问是否可以将cornerRadius应用于分组单元格。 - Sumit Gera

5

谁说在 grouped 样式的 tableView 中使用 [_tblView.layer setCornerRadius:10.0]; 不起作用。

使用这段代码,您可以使 setCornerRadius 在 grouped tableView 中正常工作。

[_tblView setBackgroundView:nil];
[_tblView setBackgroundColor:[UIColor greenColor]];
[_tblView.layer setCornerRadius:10.0];

enter image description here

[_tblView.layer setCornerRadius:10.0];不会为tableView的特定部分创建圆角,而是用于设置整个tableView的圆角。


3
如何为TableView的section设置圆角半径? - Maddy

3

不要设置单元格的圆角,而是按照以下方式设置cell.contentView的半径:

cell.contentView.layer.cornerRadius = 10.0f;
cell.contentView.layer.borderColor = [UIColor blackColor].CGColor;
cell.contentView.layer.borderWidth = 3.0f;

在初始化单元格时,将上述代码放入其中。


3

将这段代码添加到你的 .h 文件中

#import <QuartzCore/QuartzCore.h>

在你的代码中使用以下内容:

tblView.layer.cornerRadius=5.0f;

2

将QuartzCore框架添加到您的项目中

import QuartzCore/QuartzCore.h to your .h file

self.tableView.layer.cornerRadius = 5.0f;


2
我认为您缺少这行代码:
[self.tableView.layer setMasksToBounds:YES];

2

首先,您需要使用QuartzCode框架导入该框架,并声明.h文件,以设置所需的图层效果类。

然后添加该方法。

myTableView.layer.cornerRadius=5;
[myTableView setClipsToBounds:YES];

如果您想将圆角半径设置为单元格,也可以尝试以下代码:

UITableViewCell.layer是CALayer类的一种类型,而CALayer类有一个名为cornerRadius的属性。因此,您可以按照以下方式设置单元格的圆角半径:

[cell.layer setCornerRadius:3.0];

试试这段代码。


2
你可以这样做。它对我有效。
    UILabel *delLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 44)];
    delLbl.font = [UIFont fontWithName:@"Arial-BoldMT" size:18];
    delLbl.layer.cornerRadius = 10.0f;
    delLbl.layer.borderWidth = 1.0f;
    delLbl.layer.borderColor = [UIColor grayColor].CGColor;
    delLbl.text = @"Cell Text";
    delLbl.textAlignment = NSTextAlignmentCenter;
    [cell.contentView addSubview:delLbl];
    cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
    Return Cell;

2

1
尝试这个:-
tableView.layer.cornerRadius=5.0f;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接