IOS第八天(1:UITableViewController团购,数据转模型,xib显示数据)
******HMTg.h 模型数据
#import <Foundation/Foundation.h> @interface HMTg : NSObject @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *icon; @property (nonatomic, copy) NSString *price; @property (nonatomic, copy) NSString *buyCount; - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)tgWithDict:(NSDictionary *)dict; + (NSArray *)tgs; @end
******HMTg.m 模型数据
#import "HMTg.h" @implementation HMTg - (instancetype)initWithDict:(NSDictionary *)dict { self = [super init]; if (self) { [self setValuesForKeysWithDictionary:dict]; } return self; } + (instancetype)tgWithDict:(NSDictionary *)dict { return [[self alloc] initWithDict:dict]; } + (NSArray *)tgs { NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil]]; NSMutableArray *arrayM = [NSMutableArray array]; for (NSDictionary *dict in array) { [arrayM addObject:[self tgWithDict:dict]]; } return arrayM; } @end
*********cell的文件HMTgCell.h
#import <UIKit/UIKit.h> @interface HMTgCell : UITableViewCell @property (weak, nonatomic) IBOutlet UIImageView *iconView; @property (weak, nonatomic) IBOutlet UILabel *titleLabel; @property (weak, nonatomic) IBOutlet UILabel *priceLabel; @property (weak, nonatomic) IBOutlet UILabel *buyCountLabel; @end
****HMTgCell.m
#import "HMTgCell.h" @interface HMTgCell() @end @implementation HMTgCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code } return self; } - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end
**********HMViewController.m
#import "HMViewController.h" #import "HMTg.h" #import "HMTgCell.h" @interface HMViewController () @property (nonatomic, strong) NSArray *tgs; @end @implementation HMViewController - (NSArray *)tgs { if (_tgs == nil) _tgs = [HMTg tgs]; return _tgs; } - (void)viewDidLoad { [super viewDidLoad]; self.tableView.rowHeight = 80; // 调整边距,可以让表格视图让开状态栏 self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0); } ///** 隐藏状态栏 */ //- (BOOL)prefersStatusBarHidden //{ // return YES; //} #pragma mark - 数据源方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.tgs.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 1. 可重用标示符 static NSString *ID = @"Cell"; // 2. tableView查询可重用Cell HMTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 3. 如果没有可重用cell if (cell == nil) { // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; // 从XIB加载自定义视图 cell = [[[NSBundle mainBundle] loadNibNamed:@"HMTgCell" owner:nil options:nil] lastObject]; } // 4. 设置Cell内容 // 1> 取出模型 HMTg *tg = self.tgs[indexPath.row]; // cell.textLabel.text = tg.title; // cell.imageView.image = [UIImage imageNamed:tg.icon]; // // cell.detailTextLabel.text = [NSString stringWithFormat:@"¥%@ 已有%@人购买", tg.price, tg.buyCount]; cell.titleLabel.text = tg.title; cell.iconView.image = [UIImage imageNamed:tg.icon]; cell.priceLabel.text = tg.price; cell.buyCountLabel.text = tg.buyCount; return cell; } @end
没有失败, 只有放弃...