等高的cell
storyboard自定义cell
- 1.创建一个继承自UITableViewCell的子类,ZJDealTableViewCell
2.在storyboard中
往cell里面增加需要用到的子控件
设置cell的重用标识
- 设置cell的class为ZJDealModel
- 3.在控制器中
- 利用重用标识找到cell
- 给cell传递模型数据
- 4.在ZJDealModel中
- 将storyboard中的子控件连线到类扩展中
- 需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件上
- 将storyboard中的子控件连线到类扩展中
代码案例如下:
1.主控制器 ZJDealViewController 这个是继承UITableViewController的. 同时在storyboard中拖一个UITableViewController的控件,并指定Class为ZJDealViewController.
“`
#import “ZJDealViewController.h”
#import “ZJDealTableViewCell.h”
#import “ZJDealModel.h”
@interface ZJDealViewController ()
/* 存放数据的数组 /
@property (strong, nonatomic) NSArray *dealArr;
@end
@implementation ZJDealViewController
/**
* 懒加载
*/
- (NSArray *)dealArr
{
if (_dealArr == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Deal.plist" ofType:nil];
NSArray *dictArr = [NSArray arrayWithContentsOfFile:path];
/**
字典数组 转模型数组
*/
// 临时数组,存放模型的
NSMutableArray *tempArr = [NSMutableArray array];
for (NSDictionary *dict in dictArr) {
ZJDealModel *dealModel = [ZJDealModel dealWithDict:dict];
[tempArr addObject:dealModel];
}
_dealArr = tempArr;
}
return _dealArr;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dealArr.count;
}
- (ZJDealTableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @”cell”;
ZJDealTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 取出模型
// self.dealArr[indexPath.row]
// 传递模型
cell.dealModel = self.dealArr[indexPath.row]; // 这个时候需要在自定义cell中重写dealModel的set方法
return cell;
}
@end
“`
- 模型ZJDealModel.h
“`
@interface ZJDealModel : NSObject
// 购买人数
@property (strong, nonatomic) NSString *buyCount;
// 价格
@property (strong, nonatomic) NSString *price;
// 标题
@property (strong, nonatomic) NSString *title;
// 图标
@property (strong, nonatomic) NSString *icon;
// 类方法
+ (instancetype)dealWithDict:(NSDictionary *)dict;
// ZJDealModel.m
+ (instancetype)dealWithDict:(NSDictionary *)dict
{
// 初始化
ZJDealModel *deal = [[self alloc] init];
// KVC key value coding 键值编码
[deal setValuesForKeysWithDictionary:dict];
return deal;
}
“`
- 自定义的cell ZJDealTableViewCell.h
@class ZJDealModel;
@interface ZJDealTableViewCell : UITableViewCell
/** ZJDealMode 模型属性 */
@property (strong, nonatomic) ZJDealModel *dealModel;
ZJDealTableViewCell.m
#import "ZJDealTableViewCell.h"
#import "ZJDealModel.h"
@interface ZJDealTableViewCell ()
// 这些属性都是在storyboard中拖到控制器的.
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyLabel;
@end
@implementation ZJDealTableViewCell
// 重写set方法
- (void)setDealModel:(ZJDealModel *)dealModel
{
_dealModel = dealModel;
self.iconView.image = [UIImage imageNamed:dealModel.icon];
self.titleLabel.text = [NSString stringWithFormat:@"%@",dealModel.title];
self.priceLabel.text = [NSString stringWithFormat:@"¥%@",dealModel.price];
self.buyLabel.text = [NSString stringWithFormat:@"%@人已经购买",dealModel.buyCount];
}
@end