addressBookHelpercell的定制

来源:互联网 发布:安全气囊碰撞数据 编辑:程序博客网 时间:2024/06/01 07:53
@protocol AddressBookCellDelegate <NSObject>- (void)didClickCallButtonAtIndexPath:(NSIndexPath *)indexPath;@end@interface AddressBookCell : UITableViewCell@property (nonatomic, retain) UIImageView *photoView;@property (nonatomic, retain) UILabel *nameLabel;@property (nonatomic, retain) UILabel *phoneNumberLabel;@property (nonatomic, retain) UIButton *callButton;@property (nonatomic, retain) NSIndexPath *indexPath;//存储cell的位置@property (nonatomic, retain) AddressPerson *person; //存储数据源对象@property (nonatomic, assign) id<AddressBookCellDelegate> delegate;@end<pre name="code" class="objc">@implementation AddressBookCell//重写setPerson方法,给对应的控件赋值.- (void)setPerson:(AddressPerson *)person{    if (_person != person) {        [_person release];        _person = [person retain];    }    //给对应的控件赋值    self.photoView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:person.imageName ofType:@"png"]];    self.nameLabel.text = person.name;    self.phoneNumberLabel.text = person.phoneNumber;}- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if (self) {        // Initialization code        //photoView        CGFloat px = kPhotoView_MarginLeft;        CGFloat py = kPhotoView_MarginTop;        CGFloat width = kPhotoView_Width;        CGFloat height = kPhotoView_Height;        self.photoView = [[UIImageView alloc] initWithFrame:CGRectMake(px, py, width, height)];        /**         *  1.新建一个CAShaperLayer 对象            2.新建一个UIBezierPath 对象            3.绘制贝塞尔曲线路径            4.将贝塞尔曲线路径赋值给layer.            5.然后将该layer对象添加到一个视图的layer上.         */        CAShapeLayer *layer = [CAShapeLayer layer];        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.photoView.bounds cornerRadius:kCornerRadius];        layer.frame = _photoView.bounds;        layer.path = bezierPath.CGPath;        _photoView.layer.mask = layer;//        _photoView.layer.cornerRadius = kCornerRadius;//        _photoView.layer.masksToBounds = YES;        [self.contentView addSubview:_photoView];        [_photoView release];        //nameLabel        px = px + width + kMarginBetween;        py = kMarginTop;        width = kNameLabel_Width;        height = kHeight;        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(px, py, width, height)];        [self.contentView addSubview:_nameLabel];        [_nameLabel release];        //phoneNumberLabel        px = px + width;        py = py;        width = kPhoneNumberLabel_Width;        height = height;        self.phoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(px, py, width, height)];        _phoneNumberLabel.textAlignment = NSTextAlignmentCenter;        _phoneNumberLabel.font = [UIFont systemFontOfSize:15];        _phoneNumberLabel.textColor = [UIColor lightGrayColor];        [self.contentView addSubview:_phoneNumberLabel];        [_phoneNumberLabel release];        //callButton        px = px + width;        py = py;        width = kCallView_Width;        height = height;        self.callButton = [UIButton buttonWithType:UIButtonTypeCustom];        [_callButton setImage:[UIImage imageNamed:@"action_call"] forState:UIControlStateNormal];        [_callButton addTarget:self action:@selector(call:) forControlEvents:UIControlEventTouchUpInside];        _callButton.frame = CGRectMake(px, py, width, height);        [self.contentView addSubview:_callButton];    }    return self;}- (void)call:(UIButton *)btn{    if ([self.delegate respondsToSelector:@selector(didClickCallButtonAtIndexPath:)]) {        [self.delegate didClickCallButtonAtIndexPath:self.indexPath];    }}- (void)awakeFromNib{    // Initialization code}- (void)setSelected:(BOOL)selected animated:(BOOL)animated{    [super setSelected:selected animated:animated];    // Configure the view for the selected state}- (void)dealloc{    RELEASE_SAFE(_indexPath);    RELEASE_SAFE(_photoView);    RELEASE_SAFE(_nameLabel);    RELEASE_SAFE(_phoneNumberLabel);    RELEASE_SAFE(_callButton);    [super dealloc];}@end


0 0
原创粉丝点击