2013-07-14 25 views
0

我正在創建UITableViewController的單元格。 其中之一,包含一個小圖像。我嘗試以下,以使其邊角圓潤:我不能讓圖像和按鈕四角變圓

profileImage.layer.cornerRadius = 8; 
profileImage.clipsToBounds = YES; 

在另一個細胞的原型,我試圖讓圓潤的按鈕的角落:

chooseImageFromRoll.clipsToBounds = YES; 
    chooseImageFromRoll.layer.cornerRadius = 8; 

在這兩種情況下,我包括

#import <QuartzCore/QuartzCore.h> 

該按鈕和角落必須圓角的圖像是擁有它們的UITableViewController的屬性:

#import <UIKit/UIKit.h> 

@interface profileRegistrationCellVC : UITableViewCell 
@property (weak, nonatomic) IBOutlet UIImageView *profileImage; 
@property (weak, nonatomic) IBOutlet UIButton *chooseImageFromRoll; 
@property (weak, nonatomic) IBOutlet UIButton *shootImage; 

@end 

在相對的.m類:

#import "profileRegistrationCellVC.h" 
#import <QuartzCore/QuartzCore.h> 

@implementation profileRegistrationCellVC 
@synthesize profileImage; 
@synthesize chooseImageFromRoll; 
@synthesize shootImage; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
     chooseImageFromRoll.clipsToBounds = YES; 
     chooseImageFromRoll.layer.cornerRadius = 8; 
     shootImage.layer.cornerRadius = 8; 

     profileImage.layer.cornerRadius = 8; 
     profileImage.clipsToBounds = YES; 

     profileImage.layer.borderColor = [UIColor whiteColor].CGColor; 
     profileImage.layer.borderWidth = 20.0; 

     [self addSubview:profileImage]; 
     [self addSubview:chooseImageFromRoll]; 
     [self addSubview:shootImage]; 


    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

@end 

這裏是我的函數的cellForRowAtIndexPath的代碼,在我的UITableViewController:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (indexPath.section == 0) { 
     static NSString *CellIdentifier = @"profileRegistrationCell"; 

     profileRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[profileRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 

     //cell.profileImage.image.layer.masksToBounds = YES; 
     //cell.profileImage.layer.cornerRadius = 5.0; 

     return cell; 

    } 

    else if (indexPath.section == 1) { 
     static NSString *CellIdentifier = @"regularRegistrationCell"; 

     regularRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[regularRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 

     cell.regularFieldName.text = [_registrationItems objectAtIndex:indexPath.row]; 

     if ([[_registrationItems objectAtIndex:indexPath.row] isEqualToString:@"Email*"]) 
      cell.regularTextField.keyboardType = UIKeyboardTypeEmailAddress; 

     if ([[_registrationItems objectAtIndex:indexPath.row] isEqualToString:@"Età"]) { 
      cell.regularTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; 
     } 

     return cell; 

    } 

    else{ 
     static NSString *CellIdentifier = @"orientationRegistrationCell"; 

     orientationRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[orientationRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 

     cell.fieldLabel.text = [_registrationItems objectAtIndex:[_registrationItems count]-1]; 
     cell.orientationLabel.text = @"Non specificato"; 
     return cell; 

    } 
} 

但在任何情況下,我設法讓圓角。你能說出我錯在哪裏嗎?

謝謝

+0

請檢查編輯答案。 –

+0

我提供的代碼首先用於創建一個按鈕,第二個用於圓角uiimage的角落。通過使用兩個蒙版並定義圓角半徑,如果您在訪問您的自定義單元格的cellfprrowatindexpath方法中使用它,它應該可以工作。在返回單元格之前,只需在方法內添加蒙版或半徑方法即可。也請張貼您的cellviewrow索引路徑的tableview,所以我可以看到你做了什麼。 –

+0

我編輯我的帖子以添加cellForRowAtIndexPath代碼。 我忘了指定從故事板創建的按鈕和圖像。後者已經有背景顏色和標題,但我通過故事板界面指定它們是自定義按鈕。這一切都會影響我的代碼嗎?我的代碼變得無效了嗎?此外感謝 – ubisum

回答

3

上面的代碼片段都是正確的,沒有問題。我的假設是,你的問題在於創建包含圖像的按鈕。下面的代碼將創建一個按鈕,並將其四捨五入,我也添加了邊框以防萬一你想添加。你也可以插入你在那裏的圖片。我已經添加了這個代碼將爲您的再次創建的圖像。希望它能幫助你。

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btn.frame = CGRectMake(100, 100, 100,50); 
    [btn setTitle:@"Hello" forState:UIControlStateNormal]; 
    [btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:0.7]]; 
    btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same value 
    btn.clipsToBounds = YES; 

    btn.layer.cornerRadius = 20;//half of the width 
    btn.layer.borderColor=[UIColor redColor].CGColor; 
    btn.layer.borderWidth=2.0f; 

    [self.view addSubview:btn]; 

下面是與上面的代碼相關的按鈕的圖像

enter image description here

編輯:

做圓角的另一種方法是使用方法masksToBounds這裏就是它在通用單元格中的一個例子,從模板開始。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

NSDate *object = _objects[indexPath.row]; 
cell.textLabel.text = [object description]; 
cell.imageView.image = [UIImage imageNamed:@"acolon.png"]; 
cell.imageView.layer.masksToBounds = YES; 
cell.imageView.layer.cornerRadius = 5.0; 
return cell; 
} 

這裏是結果的屏幕截圖:

enter image description here

我知道你使用自定義的電池,因此實現在cellForRowAtImdexPathmaskToBount或任何你正在填充tableview其定製細胞。

+0

謝謝你的回答 圖像和角落必須做圓角的按鈕是包含它們的UITableViewCell的屬性。這會改變什麼嗎?我可以將您的代碼創建的按鈕分配給我擁有的那個按鈕嗎?如何,在這種情況下?謝謝 – ubisum

+0

我編輯我的問題,使事情更清晰 – ubisum

+0

我想你的第一個代碼塊更適合我的需要。 我複製粘貼它,我嘗試了兩種不同的方法:第一,在你的代碼結束後,我設置chooseImageFromRoll = btn,其中chooseImageFromRoll是我的插座按鈕。 然後,我嘗試用chooseImageFromRoll替換每個出現的btn。沒有解決方案 – ubisum