2013-08-07 112 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; 

把上面的代碼而初始化的單元格。