整体文件的结构
ViewController.m文件==============================
// TG团购
//
// Created by Toge on 6/5/15.
// Copyright (c) 2015 wxhl. All rights reserved.
//
#import "ViewController.h"
#import "TGtg.h"
#import "TGtgCell.h"
@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutletUITableView *tableView;
@end
@implementation ViewController
- (NSArray *)tgs
{
if (_tgs ==nil) {
// UINib *nib = [UINib nibWithNibName:@"tgs" bundle:nil];
// NSArray *array = [nib instantiateWithOwner:nil options:nil];
NSString *path = [[NSBundlemainBundle]pathForResource:@"tgs"ofType:@"plist"];
NSArray *array = [NSArrayarrayWithContentsOfFile:path];
NSMutableArray *tempArray = [[NSMutableArrayalloc]initWithCapacity:15];
for (NSDictionary *dict in array) {
TGtg *tg = [TGtgtgWithDict:dict];
[tempArray addObject:tg];
}
_tgs = tempArray;
}
return _tgs;
}
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableView.rowHeight =80;
}
//先把电池栏隐藏
- (UIStatusBarStyle)preferredStatusBarStyle
{
return YES;
}
#pragma mark - datasource
//加载多少组数据 ,此处只有一组可以省略
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//
//}
//加载多少数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tgs.count;
}
//创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//调用封装的类,返回需要的cell,控制器并不需要知道,cell中有那些数据
TGtgCell *cell = [TGtgCelltgWithTableView:tableView];
//获得对应的模型
TGtg *tg = self.tgs[indexPath.row];
//将活的的模型传给,封装类,实现的过程控制器并不需要知道
cell.tg = tg;
return cell;
}
@end
TGtgCell.m=======================================
// TG团购
//
// Created by Toge on 6/5/15.
// Copyright (c) 2015 wxhl. All rights reserved.
//
#import "TGtgCell.h"
#import "TGtg.h"
@interface TGtgCell ()
@property (weak, nonatomic) IBOutletUIImageView *iconView;
@property (weak, nonatomic) IBOutletUILabel *titleLabel;
@property (weak, nonatomic) IBOutletUILabel *priceLabel;
@property (weak, nonatomic) IBOutletUILabel *buyCountLabel;
@end
@implementation TGtgCell
//封装nib文件的加载,直接返回cell
+ (instancetype)tgWithTableView:(UITableView *)tableView
{
//定义标识符
static NSString *identify =@"tg";
//在内存中找对应的cell
TGtgCell *cell = [tableViewdequeueReusableHeaderFooterViewWithIdentifier:identify];
//判断cell是否存在,不存在就重新创建
if (cell == nil) {
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identify];
cell = [[[NSBundlemainBundle] loadNibNamed:@"TGtgcell"owner:niloptions:nil]lastObject];
}
return cell;
}
//将此tableviw的subview的赋值进行封装封装,将模型传进来
- (void)setTg:(TGtg *)tg
{
_tg = tg; //这不一定要有
self.iconView.image = [UIImageimageNamed:tg.icon];
self.titleLabel.text = tg.title;
self.priceLabel.text = [NSStringstringWithFormat:@"¥%@",tg.price];
self.buyCountLabel.text = [NSStringstringWithFormat:@"%@人购买",tg.buyCount];
}
@end
// TGtgCell.h========================================
// TG团购
//
// Created by Toge on 6/5/15.
// Copyright (c) 2015 wxhl. All rights reserved.
//
#import <UIKit/UIKit.h>
@class TGtg;
@interface TGtgCell :UITableViewCell
//封装读取nib文件
+ (instancetype)tgWithTableView:(UITableView *)tableview;
//将模型传给view初始化此view中的各subview
@property (nonatomic,strong)TGtg *tg;
@end
// TGtg.m===========================================
// TG团购
//
// Created by Toge on 6/5/15.
// Copyright (c) 2015 wxhl. All rights reserved.
//
#import "TGtg.h"
@implementation TGtg
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [superinit]) {
//KVC方式给模型赋值
[selfsetValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)tgWithDict:(NSDictionary *)dict
{
return [[selfalloc]initWithDict:dict];
}
@end
// TGtg.h=================================
// TG团购
//
// Created by Toge on 6/5/15.
// Copyright (c) 2015 wxhl. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface TGtg : NSObject
//模型的属性,和对应字典中的key名字相同
@property (nonatomic,copy)NSString *title;
@property (nonatomic,copy)NSString *price;
@property (nonatomic,copy)NSString *icon;
@property (nonatomic,copy) NSString *buyCount;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)tgWithDict:(NSDictionary *)dict;
@end
最后效果图