视图需要我们自己去定义样式时用到了TableViewCell组件,效果如下
首先创建View Based App工程,在.xib文件中拖入一个Table View,前面我们说到了,这里就不再重复,注意连接协议和两个必须方法的实现。完成.h中代码
- #import <UIKit/UIKit.h>
- @interface TableViewCellViewController : UIViewController
- <UITableViewDelegate,UITableViewDataSource>{
- IBOutlet UITableView *tView;
- }
- @property (nonatomic,retain)UITableView *tView;
- @end
完成.m中代码
- #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
若不需要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文件
双击MyCell.xib,将Table View Cell拖入主窗口中,并且删除原主窗口中的View图标
在.h文件中完成代码
- #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
在.m中完成代码
- #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
最后我们看一下MyCell.xib中的连接,按住Ctrl拖入将要显示区域的文字和图片找到相应的接口即可。
(注意:是myCell和文字和图片连接,不是file’s owner和文字和图片连接,我又犯这个错误了)