UI09_自定义cell
来源:互联网 发布:方正小标宋简体 mac 编辑:程序博客网 时间:2024/05/18 00:38
准备工作:定义宏#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];创建上面的labelself.upLable=[[UILabel alloc]init];self.upLable.backgroundColor=[UIColor redColor];[self.contentView addSubview:self.upLable];[_upLable release];创建下面的labelself.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; }}
0 0
- UI09_自定义cell
- iOS UI09_自定义cell
- UI09_自定义cell
- UI09_自定义cell
- UI09_自定义cell写通讯录
- UI09_重写Cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- HD 1513 Palindrome 【LCS】
- [c语言]冒泡排序升级版------选择排序简单版
- mac os OS X 优山美地(Yosemite)如何提高最大连接数限制
- C语言-Pointer(high)AndMacro
- Installshield安装
- UI09_自定义cell
- java连接mysql数据库
- spring读取资源文件时自定义数据类型转换---CustomEditorConfigurer
- poj 1260 Pearls dp
- Advanced Fruits(HDU1503)LCS
- swap清内存
- acm 输入输出外挂
- 19个必须知道的Visual Studio快捷键
- HDOJ--1503 Advanced Fruits