自定义UITableViewCell
2015-06-10 14:17
423 查看
介绍两种方法
第一种,
简单的增加UITableViewCell一些小功能
例如在cell上面添加一个UILabel。
直接在UITableViewCell的生成方法中实现,代码如下
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(190, 0, 130, cell.frame.size.height)];
label1.tag = 1;
[cell.contentView addSubview:label3];
[label3 release];
}
UILabel *label3 = (UILabel *)[cell.contentView viewWithTag:1];
label1.text = @"44444";
return cell;
}
第二种,较为正规的写法。
新建一个自定义的继承UITableViewCell的类如NewCell。
在NewCell中增加两个UILabel的属性
代码如下
//NewCell.h
#import
@interface NewCell : UITableViewCell
{
UILabel *_label1;
UILabel *_label2;
}
- (void)setLabel1Text:(NSString *)text1
label2Text:(NSString *)text2;
@end
//NewCell.m
#import "NewCell.h"
@implementation NewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160,self.frame.size.height)];
_label1.text = @"111111111";
[self.contentView addSubview:_label1];
_label2 = [[UILabel alloc] initWithFrame:CGRectMake(160, 0, 160,self.frame.size.height)];
_label2.text = @"111111111";
[self.contentView addSubview:_label2];
}
return self;
}
- (void)setLabel1Text:(NSString *)text1
label2Text:(NSString *)text2
{
_label1.text = text1;
_label2.text = text2;
}
- (void)dealloc
{
[_label1 release];
[_label2 release];
[super dealloc];
}
@end
//UITableViewCell的生成方法
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
NewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[NewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
[cell setLabel1Text:@"222222222" label2Text:@"333333333"];
return cell;
}
第一种,
简单的增加UITableViewCell一些小功能
例如在cell上面添加一个UILabel。
直接在UITableViewCell的生成方法中实现,代码如下
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(190, 0, 130, cell.frame.size.height)];
label1.tag = 1;
[cell.contentView addSubview:label3];
[label3 release];
}
UILabel *label3 = (UILabel *)[cell.contentView viewWithTag:1];
label1.text = @"44444";
return cell;
}
第二种,较为正规的写法。
新建一个自定义的继承UITableViewCell的类如NewCell。
在NewCell中增加两个UILabel的属性
代码如下
//NewCell.h
#import
@interface NewCell : UITableViewCell
{
UILabel *_label1;
UILabel *_label2;
}
- (void)setLabel1Text:(NSString *)text1
label2Text:(NSString *)text2;
@end
//NewCell.m
#import "NewCell.h"
@implementation NewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160,self.frame.size.height)];
_label1.text = @"111111111";
[self.contentView addSubview:_label1];
_label2 = [[UILabel alloc] initWithFrame:CGRectMake(160, 0, 160,self.frame.size.height)];
_label2.text = @"111111111";
[self.contentView addSubview:_label2];
}
return self;
}
- (void)setLabel1Text:(NSString *)text1
label2Text:(NSString *)text2
{
_label1.text = text1;
_label2.text = text2;
}
- (void)dealloc
{
[_label1 release];
[_label2 release];
[super dealloc];
}
@end
//UITableViewCell的生成方法
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
NewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[NewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
[cell setLabel1Text:@"222222222" label2Text:@"333333333"];
return cell;
}
相关文章推荐
- BlueDroid代码分析之GKI
- BlueDroid代码分析
- BlueDroid介绍
- Bluetooth数据包捕获
- cannot change ownership to uid 1000.gid 1000;operation not permitted
- iOS开发中关于UIImage的知识点总结
- UITableView去掉分隔符
- IOS中Key-Value Coding (KVC)的使用详解
- 终端(Ctrl+Alt+T)的账户是Guest,sudo和su命令都不能运行
- UITextField常用属性归纳:文本框样式、文字样式、键盘样式、左右视图样式、清除按钮设置等,iosuitextfield
- GCD学习笔记(四):dispatch_set_target_queue
- GCD学习笔记(三):Main Dispatch Queue/Global Dispatch Queue
- GCD学习笔记(二):dispatch_queue_create
- PKU 1679 The Unique MST(解题报告)
- 关于arduino的各种网站
- [转载]UIKit Dynamics入门(OC)
- iOS中UITextField用法
- setuid权限
- FreeGLUT Tips: Resolve compile issue: error LNK1104: cannot open file 'freeglutd.lib'
- quick 关于触摸的问题