iOS自定义UITableView的cell
2014-12-10 00:00
363 查看
摘要: 使用xib创建一个自定义的cell.
一,注意设置改自定义cell的标识,这样使用缓冲池来访问cell时才可以访问到.
二,声明一个继承自UITableViewCell的类,用它来管理xib(自定义cell),方便数据管理.
三,注意xib中设置class为二中的继承UITableViewCell的类,而不是UITableView类.
一,注意设置改自定义cell的标识,这样使用缓冲池来访问cell时才可以访问到.
二,声明一个继承自UITableViewCell的类,用它来管理xib(自定义cell),方便数据管理.
三,注意xib中设置class为二中的继承UITableViewCell的类,而不是UITableView类.
//-------该类用来管理MLTgCell.xib #import <UIKit/UIKit.h> @class MLTg; @interface MLTgCell : UITableViewCell //将团购模型传给cell @property(nonatomic, strong) MLTg *tg; -(void) setTg:(MLTg *)tg; +(instancetype)cellWithTableView:(UITableView *)tableView; -(instancetype)initWithTableView:(UITableView *)tableView; @end #import "MLTgCell.h" #import "MLTg.h" @interface MLTgCell() @property (weak, nonatomic) IBOutlet UIImageView *iconView; @property (weak, nonatomic) IBOutlet UILabel *titleView; @property (weak, nonatomic) IBOutlet UILabel *priceView; @property (weak, nonatomic) IBOutlet UILabel *buyCountView; @end @implementation MLTgCell -(instancetype)initWithTableView:(UITableView *)tableView{ static NSString *ID = @"tg"; MLTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { //使用xib来创建自定义cell //---在xib中需要给cell设置标识tg,否则缓存池中取不到带有ID标识的cell. cell = [[[NSBundle mainBundle] loadNibNamed:@"MLTgCell" owner:nil options:nil] firstObject]; } return cell; } +(instancetype)cellWithTableView:(UITableView *)tableView{ return [[self alloc] initWithTableView:tableView]; } -(void) setTg:(MLTg *)tg{ _tg = tg; //设置图片 self.iconView.image = [UIImage imageNamed:tg.icon]; //设置title self.titleView.text = tg.title; //设置价格 self.priceView.text = [NSString stringWithFormat:@"¥%@", tg.price]; //设置购买的数量 self.buyCountView.text = [NSString stringWithFormat:@"%@人已购买",tg.buyCount]; } @end //----------数据模型类,用来传递数据------- #import <Foundation/Foundation.h> @interface MLTg : NSObject @property(nonatomic, copy) NSString *buyCount; @property(nonatomic, copy) NSString *icon; @property(nonatomic, copy) NSString *price; @property(nonatomic, copy) NSString *title; +(instancetype)tgWithDict:(NSDictionary *)dict; -(instancetype)initWithDict:(NSDictionary *)dict; @end #import "MLTg.h" @implementation MLTg +(instancetype)tgWithDict:(NSDictionary *)dict{ return [[self alloc]initWithDict:dict]; } -(instancetype)initWithDict:(NSDictionary *)dict{ if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; } return self; } @end //-------控制器中用来返回自定义cell的方法 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //创建cell(屏蔽了cell是通过什么方式创建的(代码或xib方式)) MLTgCell *cell = [MLTgCell cellWithTableView:tableView]; //给cell传递模型数据. cell.tg = self.tgs[indexPath.row]; return cell; }
相关文章推荐
- iOS代码自定义UITableView Cell(每个Cell的高度不一样)
- iOS开发-------模拟团购网站(UITableView 表格视图)以及自定义cell
- iOS开发:XIB之UITableView自定义cell
- iOS-UITableView 右侧箭头+取消分割线+取消选中样式+自定义分割线(原生和自定义cell)
- 墨珞随笔 - IOS - 自定义UITableView的cell - 李江辉
- IOS开发中UITableView(表视图)的滚动优化及自定义Cell
- iOS中UITableView Cell实现自定义单选功能
- iOS中的UITableView自定义Cell(模仿新浪微博)
- iOS UITableView(二)-自定义Cell
- iOS开发-UITableView自定义Cell
- Ios 自定义表示图出现的相关问题UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
- IOS-UITableView 及自定义cell
- IOS之UITableView cell自定义视图中插入Tableview
- [IOS]UITableView重用(使用多个自定义Cell)
- iOS-UI控件之UITableView(三)- 自定义不等高的cell
- iOS,UITableView详细介绍之自定义Cell(二)
- iOS项目开发小技巧 (三) --UITableView实现Cell左划删除等自定义功能
- iOS:在UITableView里使用自定义Cell的那点事(简要总结)
- 【iOS】UITableView Cell自定义单选功能
- iOS开发中实现UITableView的Cell左划删除等自定义功能