您的位置:首页 > 产品设计 > UI/UE

UI 09 自定义cell

2015-08-29 10:54 441 查看
我们能够发现系统的cell有时候不能够满足我们的要求, 这时候我们就需要自定义属于我们的cell

新建一个继承于UITableViewCell 文件.

注意, cell的属性名字绝对不能够和系统的一样

#import <UIKit/UIKit.h>

@interface ThreeANDTwoTableViewCell : UITableViewCell
@property(nonatomic, retain)UIImageView *imageviewOne;
@property(nonatomic, retain)UIImageView *imageviewTwo;
@property(nonatomic, retain)UIImageView *imageviewThree;
@property(nonatomic, retain)UILabel *leftLabel;
@property(nonatomic, retain)UILabel *rightLabel;

@end


在.m中有两个方法, 一个是初始化, 一个是layOutViews

#import "ThreeANDTwoTableViewCell.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height
@implementation ThreeANDTwoTableViewCell
- (void)dealloc{
[_imageviewOne release];
[_imageviewThree release];
[_imageviewTwo release];
[_leftLabel release];
[_rightLabel release];
[super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self creatViews];
}
return self;
}
- (void)creatViews{
// 再初始化中不设置fram
self.imageviewOne = [[UIImageView alloc] init];
self.imageviewOne.backgroundColor = [UIColor cyanColor];
[self.contentView addSubview:self.imageviewOne];
[_imageviewOne release];

self.imageviewTwo = [[UIImageView alloc] init];
self.imageviewTwo.backgroundColor = [UIColor cyanColor];
[self.contentView addSubview:self.imageviewTwo];
[_imageviewTwo release];

self.imageviewThree = [[UIImageView alloc] init];
self.imageviewThree.backgroundColor = [UIColor cyanColor];
[self.contentView addSubview:self.imageviewThree];
[_imageviewThree release];

self.leftLabel = [[UILabel alloc] init];
self.leftLabel.backgroundColor = [UIColor orangeColor];
[self.contentView addSubview:self.leftLabel];
self.leftLabel.textAlignment = NSTextAlignmentCenter;
[_leftLabel release];

self.rightLabel = [[UILabel alloc] init];
self.rightLabel.backgroundColor = [UIColor cyanColor];
[self.contentView addSubview:self.rightLabel];
self.rightLabel.textAlignment = NSTextAlignmentCenter;
[_rightLabel release];

}
- (void)layoutSubviews{
// 一定不要遗忘super!
[super layoutSubviews];
self.imageviewOne.frame = CGRectMake(0, 0,WIDTH/3, HEIGHT/3*2);
self.imageviewTwo.frame = CGRectMake(WIDTH/3, 0, WIDTH/3, HEIGHT/3*2);
self.imageviewThree.frame = CGRectMake(WIDTH/3*2, 0, WIDTH/3, HEIGHT/3*2);
self.leftLabel.frame = CGRectMake(0, HEIGHT/3*2, WIDTH/2, HEIGHT/3);
self.rightLabel.frame = CGRectMake(WIDTH/2, HEIGHT/3*2, WIDTH/2, HEIGHT/3);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: