UI022---xib自定义UITableView

来源:互联网 发布:mac怎么进qq游戏大厅 编辑:程序博客网 时间:2024/06/07 03:58

使用xib自定义UItableviewcell实现一个简单的团购应用界面布局


一、效果
这里写图片描述

三、代码示例

1.没有使用配套的类,而是直接使用xib文件控件tag值操作

数据模型部分:

YYtg.h文件

 1 // 2 //  YYtg.h 3 //  01-团购数据显示(没有配套的类) 4 // 5 //  Created by apple on 14-5-29. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import <Foundation/Foundation.h>10 #import "Global.h"11 12 @interface YYtg : NSObject13 @property(nonatomic,copy)NSString *buyCount;14 @property(nonatomic,copy)NSString *icon;15 @property(nonatomic,copy)NSString *price;16 @property(nonatomic,copy)NSString *title;17 YYinitH(tg)18 @end复制代码YYtg.m文件复制代码 1 // 2 //  YYtg.m 3 //  01-团购数据显示(没有配套的类) 4 // 5 //  Created by apple on 14-5-29. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYtg.h"10 11 @implementation YYtg12 YYinitM(tg)13 @end复制代码主控制器YYViewController.m文件复制代码 1 // 2 //  ViewController.m 3 //  01-团购数据显示(没有配套的类) 4 // 5 //  Created by apple on 15-5-29. 6 //  Copyright (c) 2015年 CB. All rights reserved. 7 // 8  9 #import "ViewController.h"10 #import "CBTg.h"11 12 @interface ViewController ()<UITableViewDataSource>13 @property(nonatomic,strong)NSArray *tg;14 @property (strong, nonatomic) IBOutlet UITableView *tableview;15 16 @end17 18 @implementation CBViewController19 20 - (void)viewDidLoad21 {22     [super viewDidLoad];23     self.tableview.rowHeight=100;24     25 }26 27 #pragma mark-  懒加载28 -(NSArray *)tg29 {30     if (_tg==nil) {31         NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil];32         NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath];33         34         NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:temparray.count];35         for (NSDictionary *dict in temparray) {36             YYtg *tg=[YYtg tgWithDict:dict];37             [arrayM addObject:tg];38         }39         _tg=[arrayM mutableCopy];40     }41     return _tg;42 }43 44 #pragma mark-数据显示45 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView46 {47     return 1;48 }49 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section50 {51     return self.tg.count;52 }53 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath54 {55     //读取xib中的数据56 //    NSArray *arrayM=[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil];57 //    UITableViewCell *cell=[arrayM firstObject];58     static NSString *identifier=@"tg";59     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];60     if (cell==nil) {61        // cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];62         cell= [[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil] firstObject];63     }64     65     YYtg *tg=self.tg[indexPath.row];66     //设置数据67     //使用tag68     UIImageView *imgv=(UIImageView *)[cell viewWithTag:1];69     imgv.image=[UIImage imageNamed:tg.icon];70     UILabel *buyCount=(UILabel *)[cell viewWithTag:4];71     buyCount.text=[NSString stringWithFormat:@"已有%@人购买",tg.buyCount];72     UILabel *title=(UILabel *)[cell viewWithTag:2];73     title.text=tg.title;74     UILabel *price=(UILabel *)[cell viewWithTag:3];75     price.text=[NSString stringWithFormat:@"$%@",tg.price];76     77     78     //返回cell79     return cell;80 }81 82 //隐藏状态栏83 -(BOOL)prefersStatusBarHidden84 {85     return YES;86 }87 @end

使用xib自定义的UItableviewcell

这里写图片描述

代码分析:

上面的代码通过使用xib文件中各个控件的tag值,完成对每个部分数据的赋值和刷新。但是,作为主控制器,它应该知道xib文件中各个控件的tag值,它知道的是不是太多了呢?

为了解决上面的问题,我们可以为自定义的cell设置一个配套的类,让这个类来操作这个xib,对外提供接口,至于内部的数据处理,外界不需要关心,也不用关心。

改造后的代码如下:

2.使用xib和对应的类完成自定义cell的数据展示

新建一个类,用来管理对应的xib文件

注意类的继承类,并把该类和xib文件进行关联

这里写图片描述

CBTgcell.h文件代码:

1 //
2 // CBTgcell.h
3 // 团购(使用xib和类完成数据展示)
4 //
5 // Created by apple on 15-5-29.
6 // Copyright (c) 2015年 CB. All rights reserved.
7 //
8
9 #import

0 0