2013-08-07 108 views
6

我试过使用下面的代码,但没有运气。有人知道如何在iOS 6中做到这一点?我不要想创建一个自定义单元格。更改iOS6中uitableview的圆角半径

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

编辑:

看来,所发生的情况是,这个代码创建整个视图,不是每一个人UITableViewSection一个拐角半径。这有意义吗?

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

enter image description here

+0

查看我的更新回答.. – Jitendra

+0

请问为什么你不想要自定义单元格?它让生活变得如此简单 –

+0

@RobvanderVeer我以前在使用自定义单元格时遇到了困难,但我想我会接受使用自定义单元格的建议。我主要担心性能问题。人们建议使用整个单元格的自定义图像,这似乎是一个懒惰和不正确的方式来解决这个问题。 – Apollo

回答

19

可以更改TableViewCell的德backgroundView,创建的UIView的一个子类,并改变图层类在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; 

结果如下:

enter image description here

您可以修改你想要的角落或创建另一个路径。

我希望它有帮助。

+0

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

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]; 

,如果你想设置的角落radious细胞也试过这个代码

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; 
1

试试这个: - :

@interface BackgroundView : UIView 
@end 

@implementation BackgroundView 

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

tableView.layer.cornerRadius=5.0f; 
3

添加到您的.h文件中

#import <QuartzCore/QuartzCore.h> 

并在代码中使用以下,

tblView.layer.cornerRadius=5.0f; 
4

谁说[_tblView.layer setCornerRadius:10.0];没有在分组风格的tableView工作。

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

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

enter image description here

[_tblView.layer setCornerRadius:10.0];不会创建的tableView的特定部分,这是用于设置整个的tableView的拐角半径转弯半径。

+1

如何为tableview部分设置圆角半径? – Maddy

3

而不是设置的小区的拐角半径,设置半径为cell.contentview如下:

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

把上面的代码而初始化的单元格。