UI09_自定义cell

准备工作:定义宏
#define  WIDTH self.contentView.frame.size.width
#define  HEIGHT self.contentView.frame.size.height

1.建立一个UITableViewCell的类MyCell

现在要给定义的cell加上4条属性,而且需要在外部进行赋值,所以在.h写属性的声明.而且这四个属性,它们的名不能和系统的已有的属性名重复,包括imageView,textLabel,datailTextLabel
@property (nonatomic, retain)UIImageView *leftImageView;
@property (nonatomic,retain)UILabel *upLable;
@property (nonatomic,retain)UILabel *downLable;
@property (nonatomic,retain)UIImageView *rightImageView;

2.UITableViewCell自己的初始化方法

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
    //完成对属性视图的创建,但是一般创建之后不给属性视图frame.属性我们会最后再layout方法里进行设置
    [self createView];
    }
    return self;
}

3.初始化方法中的createView(属性视图进行创建)

注意:self.contentView是cell专用的添加cell视图的关键词
    既cell上有一个专门用来显示控件视图,叫contentView,我们把视图就放到contentView显示
创建左视图
 self.leftImageView=[[UIImageView alloc]init];
 self.leftImageView.backgroundColor=[UIColor grayColor];
 [self.contentView addSubview:self.leftImageView];
 [_leftImageView release];
创建右视图
self.rightImageView=[[UIImageView alloc]init];
self.rightImageView.backgroundColor=[UIColor grayColor];
[self.contentView addSubview:self.rightImageView];
[self.rightImageView release];
创建上面的label
self.upLable=[[UILabel alloc]init];
self.upLable.backgroundColor=[UIColor redColor];
[self.contentView addSubview:self.upLable];
[_upLable release];
创建下面的label
self.downLable=[[UILabel alloc]init];
self.downLable.backgroundColor=[UIColor blueColor];
[self.contentView addSubview:self.downLable];
[_upLable release];

4.layoutSubviews方法(cell显示之前的最后一个方法)

前述:一般会在这个方法里设置所有的属性视图的大小和尺寸,这个方法会用在图片文字的自适应的设置上
    这里我们要特别注意要调用父类方法 继承父类的基础上设置
-(void)layoutSubviews
{
    //重写了父类的layoutSubview方法,如果想要这个方法发挥正常的功能,别忘了[super layoutSubViews];
    [super layoutSubviews];
    //对所有属性视图的位置和大小设置
    self.leftImageView.frame=CGRectMake(0, 0, WIDTH/3,HEIGHT);
    self.upLable.frame=CGRectMake(WIDTH/3, 0, WIDTH/3, HEIGHT/2);
    self.downLable.frame=CGRectMake(WIDTH/3, HEIGHT/2, WIDTH/3, HEIGHT/2);
    self.rightImageView.frame=CGRectMake(WIDTH/3*2, 0, WIDTH/3, HEIGHT);
}

5.内存释放

- (void)dealloc
{
    [_rightImageView release];
    [_leftImageView release];
    [_upLable release];
    [_downLable release];
    [super dealloc];
}
类似方法我们在定义一个MyCell2

在tableView上显示cell

别忘记头文件
首先我们建立tableView,签订协议,设置必须实现的方法  而我们要着重讲述必须实现方法中对cell设置
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row%2==1) {
        static NSString *reuse=@"reuse";
        MyCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell=[[[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
        }
        cell.upLable.text=self.arr[indexPath.row];
        cell.leftImageView.image=[UIImage imageNamed:@"drink.png"];
        cell.rightImageView.image=[UIImage imageNamed:@"cymbal.png"];
        return cell;
    }else{
        static NSString *reuse=@"newReuse";
        MyCell2 *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
             cell=[[[MyCell2 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
        }
        return cell;
    }
}

下面是tableView里的完整实现

1.定义属性
@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *arr;
2.内存释放
- (void)dealloc
{
    [_arr release];
    [_tableView release];
    [super dealloc];
}
3.定义初始化
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
    }
    return self;
}
4.创建tableView
- (void)viewDidLoad 
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor redColor];
    self.navigationController.navigationBar.translucent=NO;
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.rowHeight=200;
}
必须实现方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row%2==1) {
        static NSString *reuse=@"reuse";
        MyCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell=[[[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
        }
        cell.upLable.text=self.arr[indexPath.row];
        cell.leftImageView.image=[UIImage imageNamed:@"drink.png"];
        cell.rightImageView.image=[UIImage imageNamed:@"cymbal.png"];
        return cell;
    }else{
        static NSString *reuse=@"newReuse";
        MyCell2 *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
             cell=[[[MyCell2 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
        }
        return cell;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值