码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UI09_自定义cell

时间:2015-08-11 10:14:26      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:ui-tablevi   cell   

//
//  MyCell.h
//  UI09_自定义cell
//
//  Created by dllo on 15/8/10.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MyCell : UITableViewCell
#warning 现在要给自定义的cell加上四条属性,而且需要在外部进行赋值,所以在.h中写属性声明,而且这四个属性,他们的名字不能和系统的名字重复,包括imageView,textLabel,detailTextLabel
@property(nonatomic)UIImageView *leftImageView;
@property(nonatomic)UIImageView *rightImageView;
@property(nonatomic)UILabel *upLabel;
@property(nonatomic)UILabel *downLabel;


@end
//
//  MyCell.m
//  UI09_自定义cell
//
//  Created by dllo on 15/8/10.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "MyCell.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height
@implementation MyCell
#pragma mark 重写cell的初始化方法
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //完成对属性视图的创建,但是一般创建之后不给属性视图frame
        [self createView];

    }
    return self;
}
#pragma mark 属性视图进行创建
-(void)createView
{
    //先创建左imageView
    self.leftImageView =[[UIImageView alloc] init];
    self.leftImageView.backgroundColor=[UIColor yellowColor];
    //添加
    //contentView:cell上有一个专门用来显示控件的视图,我们把视图放到contentView上进行显示
    [self.contentView addSubview:self.leftImageView];
    [_leftImageView release];

    self.upLabel =[[UILabel alloc] init];
    self.upLabel.backgroundColor=[UIColor orangeColor];
    [self.contentView addSubview:self.upLabel];
    [_upLabel release];


    self.downLabel=[[UILabel alloc] init];
    self.downLabel.backgroundColor=[UIColor redColor];
    [self.contentView addSubview:self.downLabel];
    [_downLabel release];

    self.rightImageView=[[UIImageView alloc] init];
    self.rightImageView.backgroundColor=[UIColor yellowColor];
    [self.contentView addSubview:self.rightImageView];
    [_rightImageView release];

}
#pragma mark 这个方法是cell显示之前走的最后一个方法,一般会在这个方法里设置所有的属性视图的大小和尺寸,这个方法会用在图片文字的自适应的设置上
-(void)layoutSubviews
{
    //重写了父类的layoutSubviews方法,如果想要这个方法发挥正常功能,别忘了[super layoutSubviews]
    [super layoutSubviews];
    //对所有属性视图的位置和大小设置
    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 * 2 / 3, 0, WIDTH/3, HEIGHT);



}
-(void)dealloc
{
    [_rightImageView release];
    [_leftImageView release];
    [_upLabel release];
    [_downLabel release];
    [super dealloc];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
//
//  NewCell.h
//  UI09_自定义cell
//
//  Created by dllo on 15/8/10.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface NewCell : UITableViewCell

@property(nonatomic,retain)UIImageView *firstImageView;
@property(nonatomic,retain)UIImageView *secondImageView;
@property(nonatomic,retain)UIImageView *thirdImageView;
@property(nonatomic,retain)UILabel *firstLabel;
@property(nonatomic,retain)UILabel *secondLabel;

@end
//
//  NewCell.m
//  UI09_自定义cell
//
//  Created by dllo on 15/8/10.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "NewCell.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@implementation NewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //完成对属性视图的创建,但是一般创建之后不给属性视图frame
        [self createView];

    }
    return self;
}


-(void)createView
{
    self.firstImageView=[[UIImageView alloc] init];
    self.firstImageView.backgroundColor=[UIColor yellowColor];
    [self.contentView addSubview:self.firstImageView];
    [_firstImageView release];

    self.secondImageView=[[UIImageView alloc] init];
    self.secondImageView.backgroundColor=[UIColor redColor];
    [self.contentView addSubview:self.secondImageView];
    [_secondImageView release];

    self.thirdImageView=[[UIImageView alloc] init];
    self.thirdImageView.backgroundColor=[UIColor blueColor];
    [self.contentView addSubview:self.thirdImageView];
    [_thirdImageView release];

    self.firstLabel=[[UILabel alloc] init];
    self.firstLabel.backgroundColor=[UIColor greenColor];
    [self.contentView addSubview:self.firstLabel ];
    [self.firstLabel release];

    self.secondLabel=[[UILabel alloc] init];
    self.secondLabel.backgroundColor=[UIColor cyanColor];
    [self.contentView addSubview:self.secondLabel ];
    [_secondLabel release];


}
-(void)dealloc
{
    [_firstImageView release];
    [_secondImageView release];
    [_thirdImageView release];
    [_firstLabel release];
    [_secondLabel release];
    [super dealloc];
}

-(void)layoutSubviews
{
    self.firstImageView.frame =CGRectMake(0, 0, WIDTH/3, HEIGHT/2);

    self.secondImageView.frame=CGRectMake(WIDTH/3, 0, WIDTH/3, HEIGHT/2);

    self.thirdImageView.frame=CGRectMake(WIDTH*2/3, 0, WIDTH/3, HEIGHT/2);

    self.firstLabel.frame=CGRectMake(0, HEIGHT/2, WIDTH/2, HEIGHT/2);

    self.secondLabel.frame=CGRectMake(WIDTH/2, HEIGHT/2, WIDTH/2, HEIGHT/2);



}





- (void)awakeFromNib {
    // Initialization code
}







- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
//
//  NewCell.m
//  UI09_自定义cell
//
//  Created by dllo on 15/8/10.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "NewCell.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@implementation NewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //完成对属性视图的创建,但是一般创建之后不给属性视图frame
        [self createView];

    }
    return self;
}


-(void)createView
{
    self.firstImageView=[[UIImageView alloc] init];
    self.firstImageView.backgroundColor=[UIColor yellowColor];
    [self.contentView addSubview:self.firstImageView];
    [_firstImageView release];

    self.secondImageView=[[UIImageView alloc] init];
    self.secondImageView.backgroundColor=[UIColor redColor];
    [self.contentView addSubview:self.secondImageView];
    [_secondImageView release];

    self.thirdImageView=[[UIImageView alloc] init];
    self.thirdImageView.backgroundColor=[UIColor blueColor];
    [self.contentView addSubview:self.thirdImageView];
    [_thirdImageView release];

    self.firstLabel=[[UILabel alloc] init];
    self.firstLabel.backgroundColor=[UIColor greenColor];
    [self.contentView addSubview:self.firstLabel ];
    [self.firstLabel release];

    self.secondLabel=[[UILabel alloc] init];
    self.secondLabel.backgroundColor=[UIColor cyanColor];
    [self.contentView addSubview:self.secondLabel ];
    [_secondLabel release];


}
-(void)dealloc
{
    [_firstImageView release];
    [_secondImageView release];
    [_thirdImageView release];
    [_firstLabel release];
    [_secondLabel release];
    [super dealloc];
}

-(void)layoutSubviews
{
    self.firstImageView.frame =CGRectMake(0, 0, WIDTH/3, HEIGHT/2);

    self.secondImageView.frame=CGRectMake(WIDTH/3, 0, WIDTH/3, HEIGHT/2);

    self.thirdImageView.frame=CGRectMake(WIDTH*2/3, 0, WIDTH/3, HEIGHT/2);

    self.firstLabel.frame=CGRectMake(0, HEIGHT/2, WIDTH/2, HEIGHT/2);

    self.secondLabel.frame=CGRectMake(WIDTH/2, HEIGHT/2, WIDTH/2, HEIGHT/2);



}





- (void)awakeFromNib {
    // Initialization code
}







- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
//
//  Student.m
//  UI09_自定义cell
//
//  Created by dllo on 15/8/10.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "Student.h"

@implementation Student
-(void)dealloc
{
    [_hobby release];
    [_name release];
    [_sex release];
    [_phone release];
    [super dealloc];
}
//用KVC一定要写这个,避免因为key和属性名不匹配造成的不必要的崩溃
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    if ([key isEqualToString:@"sex"]) {

    }
}



@end
//
//  MainViewController.m
//  UI09_自定义cell
//
//  Created by dllo on 15/8/10.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "MainViewController.h"
#import "MyCell.h"
#import "NewCell.h"
#import "Student.h";

@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *arr;


@end

@implementation MainViewController
-(void)dealloc
{
    [_tableView release];
    [_arr release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
        [self createData];
    }
    return self;
}
-(void)createData
{
    NSString *path=[[NSBundle mainBundle] pathForResource:@"StudentArr" ofType:@"plist"];
    NSArray *stuArr=[NSArray arrayWithContentsOfFile:path];

    NSDictionary *dic=stuArr[1];
    //通过KVC对model进行赋值
    Student *stu=[[Student alloc] init];
    [stu setValuesForKeysWithDictionary:dic];
    NSLog(@"%@",stu.name);


}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor yellowColor];
    self.navigationController.navigationBar.translucent=NO;

    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.rowHeight=100;
    [self.tableView release];
//    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;





}
-(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";
        NewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell =[[[NewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
        }
        cell.firstLabel.text=self.arr[indexPath.row];
        cell.firstImageView.image=[UIImage imageNamed:@"8.jpg"];
        cell.secondImageView.image=[UIImage imageNamed:@"3.jpg"];
        cell.thirdImageView.image=[UIImage imageNamed:@"2.jpg"];
        return cell;
    }else{

        static NSString *newReuse=@"newReuse";
        MyCell *cell =[tableView dequeueReusableCellWithIdentifier:newReuse];
        if (!cell) {
            cell =[[[MyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:newReuse] autorelease];
        }
        cell.upLabel.text=self.arr[indexPath.row];
        cell.leftImageView.image=[UIImage imageNamed:@"1.jpg"];
        cell.rightImageView.image=[UIImage imageNamed:@"6.jpg"];
        return cell;

    }

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
![这里写图片描述](http://img.blog.csdn.net/20150811084229594)

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS UI09_自定义cell

标签:ui-tablevi   cell   

原文地址:http://blog.csdn.net/cheng_xiansheng/article/details/47414355

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!