UITableViewCell的使用——自定义tableView视图

[size=x-large]视图需要我们自己去定义样式时用到了TableViewCell组件,效果如下[/size]
[img]http://dl.iteye.com/upload/attachment/351794/b1ce7ac1-720f-3078-89e0-8d0d95792390.png[/img]
[size=x-large]首先创建View Based App工程,在.xib文件中拖入一个Table View,前面我们说到了,这里就不再重复,注意连接协议和两个必须方法的实现。完成.h中代码[/size]
#import <UIKit/UIKit.h>
@interface TableViewCellViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>{
IBOutlet UITableView *tView;
}
@property (nonatomic,retain)UITableView *tView;
@end

[size=x-large]完成.m中代码[/size]
#import "TableViewCellViewController.h"
#import "MyCell.h"
@implementation TableViewCellViewController
@synthesize tView;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[tView release];
[super dealloc];
}
-(NSInteger) tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 9;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCellIdentifier";
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"mycell" owner:self options:nil];
cell = [array objectAtIndex:0];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}
[[cell lable] setText:@"31"];
[[cell lable1] setText:@"Raul"];
[[cell myImage] setImage:[UIImage imageNamed:@"3316.jpg"]];
return cell;
}
- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 120;
}
@end

[size=x-large]若不需要cell则改为:
static NSString *CellIdentifier2 = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] autorelease];
cell.textLabel.text =@"CCCCCCCC";
[cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:16.0f]];
// cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sign_10x12.png"]];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
[cell.textLabel setTextColor:[UIColor colorWithRed:0/255.0 green:106/255.0 blue:166/255.0 alpha:1.0]];
return cell;
下面我们新建一个类,注意subclass选择UITableViewCell,名称为MyCell,生成之后再创建相应的xib文件[/size]
[img]http://dl.iteye.com/upload/attachment/351797/237771e5-0d9c-3573-8206-47b50d605a89.png[/img]
[size=x-large]双击MyCell.xib,将Table View Cell拖入主窗口中,并且删除原主窗口中的View图标[/size]
[img]http://dl.iteye.com/upload/attachment/351803/a45501f0-a3ba-35cd-929f-d03de03f4685.png[/img]
[size=x-large]在.h文件中完成代码[/size]
#import <UIKit/UIKit.h>
@interface MyCell : UITableViewCell {
IBOutlet UILabel *lable;
IBOutlet UILabel *lable1;
IBOutlet UIImageView *myImage;
}
@property(nonatomic,retain) UILabel *lable;
@property(nonatomic,retain) UILabel *lable1;
@property (nonatomic,retain) UIImageView *myImage;
@end

[size=x-large]在.m中完成代码[/size]
#import "MyCell.h"
@implementation MyCell
@synthesize lable,lable1,myImage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[super setSelected:selected animated:animated];

// Configure the view for the selected state
}
- (void)dealloc {
[myImage release];
[lable release];
[super dealloc];
}
@end

[size=x-large]最后我们看一下MyCell.xib中的连接,按住Ctrl拖入将要显示区域的文字和图片找到相应的接口即可。[color=red](注意:是myCell和文字和图片连接,不是file’s owner和文字和图片连接,我又犯这个错误了)[/color][/size]
[img]http://dl.iteye.com/upload/attachment/352830/8a60c138-5c68-3620-8cf5-61e52ca4efa3.png[/img]

[img]http://dl.iteye.com/upload/attachment/351815/72dfcb1e-7b9f-367f-99f0-1da4fccbe37d.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值