视图控制器头文件 #WLViewController.h
#import <UIKit/UIKit.h>
@interface WLViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSArray *tuanModel;
@end
视图控制器实现 #WLViewController.m
#import "WLViewController.h"
#import "WLTuanModel.h"
#import "WLTableViewCellTuan.h"
@interface WLViewController ()<UITableViewDataSource>
@end
@implementation WLViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.dataSource=self;
self.tableView.rowHeight=80;
}
-(NSArray *)tuanModel{
if(_tuanModel==nil){
//用来保存model 对象的数组
NSMutableArray *tuans=[[NSMutableArray alloc]init];
NSArray *data=[WLTuanModel tuanLoadDataByFile];
for (NSDictionary *dict in data) {
WLTuanModel *tuan=[WLTuanModel tuanWithDict:dict];
[tuans addObject:tuan];
}
_tuanModel=tuans;
}
return _tuanModel;
}
/*interface DataSource
*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
/*interface DataSource
*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.tuanModel.count;
}
/*interface DataSource
*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//创建cell
WLTableViewCellTuan * cell=[WLTableViewCellTuan cellWithTableView:tableView];
//模型给cell分配数据
cell.tuanModel=self.tuanModel[indexPath.row];
return cell;
}
数据模型头文件 #WLTuanModel.h
#import <Foundation/Foundation.h>
@interface WLTuanModel : NSObject
@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *price;
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *buyCount;
-(instancetype)initWithDict:(NSDictionary *)data;
+(instancetype)tuanWithDict:(NSDictionary *)data;
+(NSArray *)tuanLoadDataByFile;
@end
数据模型的实现 #WLTuanModel.m
#import "WLTuanModel.h"
@implementation WLTuanModel
-(instancetype)initWithDict:(NSDictionary *)dict{
if(self=[super init]){
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+(instancetype)tuanWithDict:(NSDictionary *)dict{
return [[self alloc]initWithDict:dict];
}
+(NSArray *)tuanLoadDataByFile{
NSString *path=[[NSBundle mainBundle]pathForResource:@"tgs" ofType:@"plist"];
NSArray *tuans=[NSArray arrayWithContentsOfFile:path];
return tuans;
}
@end
自定义cell类的头文件 #WLTableViewCellTuan.h
#import <UIKit/UIKit.h>
#import "WLTuanModel.h"
@interface WLTableViewCellTuan : UITableViewCell
@property(nonatomic,strong) WLTuanModel * tuanModel;
- (void)setTuanModel:(WLTuanModel *)tuanModel;
//将创建cell的代码封装到 类的内部
+(instancetype )cellWithTableView:(UITableView *)tableView;
@end
自定义cell类的实现 #WLTableViewCellTuan.m
#import "WLTableViewCellTuan.h"
#import "WLTuanModel.h"
@interface WLTableViewCellTuan()
@property (weak, nonatomic) IBOutlet UIImageView *icon;
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *price;
@property (weak, nonatomic) IBOutlet UILabel *num;
@end
@implementation WLTableViewCellTuan
+(instancetype)cellWithTableView:(UITableView *)tableView{
static NSString *ID=@"tuangou";
WLTableViewCellTuan *cell=[tableView dequeueReusableCellWithIdentifier:ID];
if(cell==nil){
cell=[[[NSBundle mainBundle]loadNibNamed:@"WLTuanCell" owner:nil options:nil] lastObject];
}
return cell;
}
/*通过模型设置组件数据
*/
- (void)setTuanModel:(WLTuanModel *)tuanModel{
_tuanModel=tuanModel;
self.icon.image=[UIImage imageNamed:tuanModel.icon];
self.title.text=tuanModel.title;
self.price.text=[NSString stringWithFormat:@"¥ %@",tuanModel.price ];
self.num.text=[NSString stringWithFormat:@"购买数量 %@",tuanModel.buyCount ];
}
@end