改变iOS6中分组的uitableview的角半径

Apo*_*llo 7 objective-c ios

我尝试使用以下代码,但没有运气.有谁知道在iOS 6中如何做到这一点?我希望创建一个自定义单元格.

self.tableView.layer.cornerRadius = 5.0f;
    [self.tableView setClipsToBounds:YES];
Run Code Online (Sandbox Code Playgroud)

编辑:

看来实际发生的是这段代码为整个视图创建了一个角半径,而不是每个单独的UITableViewSection.这有意义吗?

我也试过[cell.layer setCornerRadius:3.0];但也没有运气.我的UITableView的角落仍然完全相同.

在此输入图像描述

jua*_*njo 19

您可以更改TableViewCell的de backgroundView,创建UIView的子类并更改图层类:

@interface BackgroundView : UIView
@end

@implementation BackgroundView

+ (Class)layerClass
{
    return [CAShapeLayer class];
}
@end
Run Code Online (Sandbox Code Playgroud)

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

结果如下:

在此输入图像描述

您可以修改所需的角点或创建另一个路径.

我希望它有所帮助.


War*_*olf 5

谁说[_tblView.layer setCornerRadius:10.0];不能在分组样式的 tableView 中工作。

编写此代码,您将设置 setCornerRadius 也适用于分组 tableView。

[_tblView setBackgroundView:nil];
[_tblView setBackgroundColor:[UIColor greenColor]];
[_tblView.layer setCornerRadius:10.0];
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

  • 如何设置表格视图部分的圆角半径? (3认同)