表格01--用户自定义tableViewCell(带有xib文件的)
来源:互联网 发布:大数据分析是什么 编辑:程序博客网 时间:2024/05/18 02:33
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.}
以上即可完成。
- 表格01--用户自定义tableViewCell(带有xib文件的)
- 【swift】自定义xib 的tableviewcell的使用
- 自定义xib tableviewcell 无法通过nsdata dataWithContentsOfFile读取的问题
- 关于xib中要TableviewCell的自定义心得
- TableViewCell指定 xib文件 调试
- 加载xib 描述的 tableViewCell
- xib 创建tableviewCell的重用
- 自定义xib TableViewCell 标准写法 代码段
- UITableView with xib 以及自定义TableViewCell
- 【iOS】使用Xib自定义tableViewCell 获取数据
- iOS 在tableViewcell的xib文件中添加新的控件 为null
- 自定义TableViewCell的使用方法
- TableViewCell组件的自定义
- 自定义TableviewCell的UIMenuController
- 用xib文件自定义UIview的实现
- swift和storyborad环境下通过xib自定义TableViewCell
- 自定义xib文件相关
- 带有自定义图片的选择文件表单。
- css继承
- 关于ffmpeg视频的渲染
- JAVA文件下载
- 注册界面生日(日期)js
- WebService之WSDL和SOAP实例(基于JAVA)
- 表格01--用户自定义tableViewCell(带有xib文件的)
- POJ_3259(Bellman-ford)
- 从request获取各种路径总结
- HNOI2002 营业额统计 (sbt)
- iOS程序设计(原书第2版)》
- Nebula level06
- 拷优(CopyU) v2.1.403.211新版本发布
- SQL优化-索引
- Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: