//-------该类用来管理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;
}
转载于:https://my.oschina.net/u/2285956/blog/354696
本文介绍了一个自定义UITableViewCell的实现过程,包括如何加载XIB文件、设置属性和展示数据模型等关键步骤。此外还提供了一个数据模型类用于传递数据。

986

被折叠的 条评论
为什么被折叠?



