使用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 <UIKit/UIKit.h>
@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