//-------该类用来管理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; }