使用storyboard 和 xib的方式基本类似,
长话短说直接上代码.
ZYTg文件
@interface ZYTg : NSObject
/** 图标 */
@property (nonatomic, copy) NSString *icon;
/** 标题 */
@property (nonatomic, copy) NSString *title;
/** 价格 */
@property (nonatomic, copy) NSString *price;
/** 购买数 */
@property (nonatomic, copy) NSString *buyCount;
@end
@implementation ZYTg
@end
ZYTgCell文件
#import
@class ZYTg;
@interface ZYTgCell : UITableViewCell
/** ZYTg模型 */
@property(nonatomic,strong)ZYTg * tg;
@end
#import "ZYTg.h"
@interface ZYTgCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@end
@implementation ZYTgCell
// 重写set方法,设置数据
- (void)setTg:(ZYTg *)tg
{
_tg = tg;
self.iconImageView.image = [UIImage imageNamed:tg.icon];
self.titleLabel.text = tg.title;
self.priceLabel.text = [NSString stringWithFormat:@"¥%@",tg.price];
self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买",tg.buyCount];
}
@end
Main.storyboard 文件
设置不同类型的cell,在storyboard中
ViewController 文件
#import "ViewController.h"
#import "MJExtension.h"
#import "ZYTgCell.h"
#import "ZYTg.h"
@interface ViewController ()
@property (nonatomic,strong) NSArray *tgs;
@end
@implementation ViewController
- (NSArray *)tgs
{
if (_tgs == nil) {
_tgs = [ZYTg mj_objectArrayWithFilename:@"tgs.plist"];
}
return _tgs;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = 70;
}
#pragma -mark 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tgs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0) {
// 创建一个重用标识,去缓存池中取ID标识的cell
static NSString *ID = @"tg";
ZYTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 设置每一行cell的数据
cell.tg = self.tgs[indexPath.row];
return cell;
}else{
return [tableView dequeueReusableCellWithIdentifier:@"test"];
}
}
@end