自定义等高的cell(xib)
2016-02-04 23:11
351 查看
/* xib自定义cell 1.创建一个继承自UITabelViewCell的子类,比如tgCell 2.创建一个xib文件(文件名建议跟cell的类名一样),比如tgCell.xib 拖一个UITabelViewCell控件; 修改cell的class属性为tgCell; 设置重用标识; 往cell中添加需要的子控件 3.在控制器中 创建cell; 给cell传递模型数据 4.在tgCell中 提供一个创建cell的类方法; 提供一个模型属性,重写该属性的set方法,设置模型数据到子控件上; */
// // tgCell.h #import <UIKit/UIKit.h> @class tgModel; @interface tgCell : UITableViewCell /** * 团购模型数据 */ @property(nonatomic,strong)tgModel *model; /** * 创建一个cell */ + (instancetype)cellWithTableView:(UITableView *)tableView; @end
// // tgCell.m #import "tgCell.h" #import "tgModel.h" @interface tgCell() @property (weak, nonatomic) IBOutlet UIImageView *iconView; @property (weak, nonatomic) IBOutlet UILabel *titleLabel; @property (weak, nonatomic) IBOutlet UILabel *priceLabel; @property (weak, nonatomic) IBOutlet UILabel *buycountLabel; @end @implementation tgCell - (void)setModel:(tgModel *)model { _model = model; //设置数据 self.iconView.image = [UIImage imageNamed:model.icon]; self.titleLabel.text = model.title; self.priceLabel.text = [NSString stringWithFormat:@"¥%@",model.price]; self.buycountLabel.text = [NSString stringWithFormat:@"%@人已购买",model.buyCount]; } + (instancetype)cellWithTableView:(UITableView *)tableView { static NSString *ID = @"cell"; tgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([tgCell class]) owner:nil options:nil] lastObject]; } return cell; } @end
// // TableViewController.m #import "TableViewController.h" #import "tgModel.h" #import "tgCell.h" @interface TableViewController () /** 所有团购数据 */ @property(nonatomic,strong) NSArray *tgs; @end @implementation TableViewController - (NSArray *)tgs { if (_tgs == nil) { // 加载plist文件中的字典数组 NSString *path = [[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil]; NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 字典数组 -> 模型数组 NSMutableArray *arrayM = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { tgModel *model = [tgModel tgWithDict:dict]; [arrayM addObject:model]; } _tgs = arrayM; } return _tgs; } - (void)viewDidLoad { [super viewDidLoad]; // //注册xib // UINib *nib = [UINib nibWithNibName:NSStringFromClass([tgCell class]) bundle:nil]; // [self.tableView registerNib:nib forCellReuseIdentifier:@"cell"]; } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.tgs.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 创建cell的过程封装到cell类中 tgCell *cell = [tgCell cellWithTableView:tableView]; //传递模型数据 cell.model = self.tgs[indexPath.row]; return cell; } @end
相关文章推荐
- C++ MFC实现基于RFID读写器的上位机软件
- 列表 块 div和table布局
- 用storyboard和Xib自定义cell常见错误
- 8.MVC框架开发(URL路由配置和URL路由传参空值处理)
- 体育会qwer
- saltstack python api 调用
- Linux网络设置
- 《重构 改善既有代码的设计》学习笔记 1
- Java生成唯一的ID
- 2016/02/04
- qwer
- vs2012与win7不兼容问题
- HDU2841 Visible Trees(容斥原理)
- Java中的堆和栈的区别
- 代理模式
- neuq oj 1009C++统计字符 C++
- 备忘录模式
- 全排列
- 面试笔试杂项积累-leetcode 101-105
- centos上nginx安装