表格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. }
以上即可完成。
相关文章推荐
- 带有自定义图片的选择文件表单。
- Iphone开发(十二)通过xib文件自定义列表中的行
- 自定义UIViewController与xib文件关系深入分析
- 让XIB的拖自定义类型控件 ,加载自定义类里的.m文件里的代码
- iOS开发——UI基础-自定义构造方法,layoutSubviews,Xib文件,利用Xib自定义View
- 自定义UIViewController与xib文件关系深入分析
- TableViewCell中自定义XIB的使用
- xib文件的使用与封装自定义view的步骤
- iOS回顾笔记(03) -- 自定义View的封装和xib文件的使用详解
- ckeditor用户自定义配置文件config.js
- 01:操作系统(centos,redhat):文件结构、目录、权限和用户
- IOS中使用.xib文件封装一个自定义View
- 用户自定义文件的元数据
- 怎样新建class类和新建带有ViewController的xib文件
- 自定义tableViewCell实现自定义表格(需代码实现)ui中tableviewcell设置成custom
- 自定义UIViewController与xib文件关系深入分析
- [C语言]删除用户自定义后缀名的所有文件
- django自定义用户认证后进不了带有@login_required的页面
- 在准备 Windows 映像时自定义默认本地用户配置文件
- 使用自定义的xib文件进行启动页面的加载