表格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.}

以上即可完成。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 8个月宝宝坐不稳怎么办 宝宝段奶不吃奶粉怎么办 3个月宝宝头睡偏了怎么办 2个月婴儿抱着睡怎么办 两个半月的宝宝睡眠少怎么办 七个月宝宝不愿意坐怎么办 一个多月的宝宝老是哭闹怎么办 宝宝头老往后仰怎么办 8个月宝宝不会爬怎么办 孩子个头长得慢怎么办 宝宝个头长得慢怎么办 婴儿个头长得慢怎么办 11个月宝宝认生怎么办 3个月宝宝认人怎么办 3个月的宝宝认生怎么办 两个月的宝宝睡觉一惊一惊怎么办 六个月宝宝不喜欢喝水怎么办 三岁宝宝尿多怎么办 一岁的宝宝尿少怎么办 一岁宝宝尿黄怎么办 三岁宝宝尿黄怎么办 5个月宝宝认生怎么办 3岁宝宝怕生胆小怎么办 2岁宝宝怕生胆小怎么办 1岁半宝宝认生怎么办 宝宝认人一直哭怎么办 七个月宝宝认人怎么办 一个月宝宝认人怎么办 2个多月宝宝认人怎么办 婴儿认人一直哭怎么办 六个月宝宝认人怎么办 十一个月宝宝断奶后辅食怎么办 6个月宝宝不会坐怎么办 7个月宝宝坐不住怎么办 三个月宝宝白天闹觉怎么办 3个月宝宝不追声怎么办 一岁宝宝垃肚子怎么办 新生儿四天没大便怎么办老放屁 新生儿四五天不大便怎么办 新生儿五天没解大便怎么办 婴儿五天没拉大便怎么办