【发布时间】:2013-07-14 12:21:10
【问题描述】:
我正在创建 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;
}
}
但在任何情况下,我都没有设法使角落变圆。你能告诉我哪里错了吗?
谢谢
【问题讨论】:
-
请检查编辑后的答案。
-
我提供的代码首先是用于创建按钮,其次是用于圆角 uiimage。如果您在访问自定义单元格的 cellfprrowatindexpath 方法中使用蒙版并定义角半径,则它应该可以工作。只需在返回单元格之前在方法内添加掩码或半径方法。还请发布您的 cellforrowat 表格视图的索引路径,以便我可以看到您做了什么。
-
我编辑了我的帖子以添加 cellForRowAtIndexPath 代码。我忘了指定我必须舍入的按钮和图像是从情节提要中创建的。后者已经有背景颜色和标题,但我通过情节提要界面指定它们是自定义按钮。这一切会影响我的代码吗?我的代码是否无效?谢谢
-
此外:我试图从 UItablecell 控制器中对角和图像进行圆角处理,因为它是包含它们的单元格。我不知道在创建特定类型的单元格的 cellForRowAtIndexPath 中这样做是否更正确
-
我移动了用于舍入我的细胞的代码。它似乎工作。谢谢;)
标签: ios uibutton uiimage calayer