@property(nonatomic, retain)UIImageView *rightImageView;
(2).在其.m文件中重写初始化方法
重写初始化方法(可以把内部的控件在该方法内创建)
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
完成对属性视图的创建,但是一般创建之后,不给属性视图frame
[self createView];
}
return self;
}
(3).创建属性视图
-(void)createView
{
创建左imageview
self.leftImageView = [[UIImageViewalloc]init];
self.leftImageView.backgroundColor = [UIColorgreenColor];
添加
// cell上有一个专门用来显示空间的视图,教contentView,我们吧视图就放到contentView上进行显示
[self.contentViewaddSubview:self.leftImageView];
[self.leftImageViewrelease];
self.rightImageView = [[UIImageViewalloc]init];
self.rightImageView.backgroundColor = [UIColorredColor];
[self.contentViewaddSubview:self.rightImageView];
[self.rightImageViewrelease];
self.upLabel = [[UILabelalloc]init];
self.upLabel.backgroundColor = [UIColoryellowColor];
[self.contentViewaddSubview:self.upLabel];
[self.upLabelrelease];
self.downLabel = [[UILabelalloc]init];
self.downLabel.backgroundColor = [UIColorcyanColor];
[self.contentViewaddSubview:self.downLabel];
[self.downLabelrelease];
也可以创建button但是点击方法需在外面设置
}
-(void)layoutSubviews
{
重写了父类的layoutSubviews方法,如果想要这个方法发挥正常功能,别忘了[super layoutSubviews]
[superlayoutSubviews];
对所有属性视图的位置和大小进行设置
self.leftImageView.frame =CGRectMake(0,0, WIDTH /3, HEIGHT);
self.upLabel.frame =CGRectMake(WIDTH /3, 0,WIDTH / 3,HEIGHT / 2);
self.downLabel.frame =CGRectMake(WIDTH /3, HEIGHT /2, WIDTH /3, HEIGHT /2);
self.rightImageView.frame =CGRectMake(WIDTH /3 * 2,0, WIDTH /3, HEIGHT);
这个方法是cell显示之前走得最后一个方法,一般会在这个方法里设置所有的属性视图的大小和尺寸,这个方法会用在图片文字的自适应的设置上
}
-(void)dealloc
{
[self.rightImageViewrelease];
[self.leftImageViewrelease];
[self.upLabelrelease];
[self.downLabelrelease];
[superdealloc];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row %2 == 1) {
static NSString *reuse =@"reuse";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (cell == nil) {
cell = [[[MyCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:reuse] autorelease];
}
cell.upLabel.text =self.arr[indexPath.row];
cell.leftImageView.image = [UIImageimageNamed:@"psb.jpeg"];
cell.rightImageView.image = [UIImageimageNamed:@"psu.jpeg"];
}
return cell;
}