您的位置:首页 > 其它

表格01--用户自定义tableViewCell(带有xib文件的)

2013-05-10 17:18 337 查看

1.新建一个single view controller

2.在项目下新建file->iOS->cocoa Touch ->Objective-C class 并使其继承UITableViewCell类MyCell01

在.h文件中声明两个输出口,即:

@property(nonatomic,retain)IBOutletUILabel *labName;

@property(nonatomic,retain)IBOutletUILabel *labGroup;

3.在该项目下新建file->ios->User Interface->Empty命名MyCell01

即MyCell01.xib

在该文件拖一个tableViewCell 控件

并且把四个lable控件拖入其中:即


打开MyCell01.xib文件使custom class 属性 中class:设定为MyCell01,然后点击file owner,进行连接labName和labGroup。

4.viewController.xib文件中拖进一个tableView,然后连接delegate 和dataSource

5.在viewController.h中实现两个协议:

<UITableViewDataSource,UITabBarDelegate>

添加:

@property(nonatomic,strong)NSArray*computers;

6.在ViewController.m中写以下方法

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSDictionary *row1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"MacBook",@"Name",@"White",@"Color", nil];
NSDictionary *row2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"MacBook Pro",@"Name",@"Sliver",@"Color", nil];
NSDictionary *row3 = [[NSDictionary alloc]initWithObjectsAndKeys:@"iMac",@"Name",@"White",@"Color", nil];
NSDictionary *row4 = [[NSDictionary alloc]initWithObjectsAndKeys:@"MacMini",@"Name",@"Red",@"Color", nil];
self.computers =[[NSArray alloc]initWithObjects:row1,row2,row3,row4, nil];
}
//1返回行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [self.computers count];
}
//2绘制表格行
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//定义两个重用
static NSString *CellTableIdentifier = @"CellTableIdentifier";
static NSString *SelfCellIdentifier = @"SelfCellIdentifier";
static BOOL nibsRegistered = NO;
NSDictionary *rowData;
NSUInteger row = [indexPath row];
if (row%2 == 0) {
if (!nibsRegistered) {
UINib *nib = [UINib nibWithNibName:@"MyXIBCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:CellTableIdentifier];
nibsRegistered = YES;
}
//测试
NSLog(@"row0 = %d",row);
rowData = [self.computers objectAtIndex:row];
MyXIBCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
cell.lblName.text = [rowData objectForKey:@"Name"];
cell.lblColor.text = [rowData objectForKey:@"Color"];
return cell;
}else{
MyCell *cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SelfCellIdentifier];
NSLog(@"row1 = %d",row);
rowData = [self.computers objectAtIndex:row];
NSLog(@"name = %@",[rowData objectForKey:@"Name"]);
cell.lblName.text = [[self.computers objectAtIndex:row]objectForKey:@"Name"];
cell.lblColor.text = [rowData objectForKey:@"Color"];
return cell;

}

}

//
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([indexPath  row]%2!=0) {
return 80;
}
return 65;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

以上即可完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: