UITableView_5-团购简单实现,nib封装实现,模型封装实现
来源:互联网 发布:js slice和substring 编辑:程序博客网 时间:2024/05/19 12:25
整体文件的结构
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
最后效果图
- UITableView_5-团购简单实现,nib封装实现,模型封装实现
- “封装”的简单实现
- JavaBean实现简单参数封装
- 系统调用的简单封装类实现
- 一个简单结构体实现封装
- Android 通用标题栏简单封装实现
- C语言实现简单的日志封装
- Fragment实现懒加载以及简单封装
- Android 客户端Socket 实现及简单封装。
- 封装简单分页类的实现
- Retrofit简单封装,实现链式编程
- 封装的多种实现
- 封装与隐藏实现。
- C# 封装SQLite实现
- 反射实现JDBC封装
- js 实现封装
- JavaScript封装实现Ajax
- C语言实现封装
- java万年历
- TCP/IP协议簇
- JMS消息服务
- 0-1背包问题
- GStreamer 1.2 on the Raspberry Pi
- UITableView_5-团购简单实现,nib封装实现,模型封装实现
- 理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)
- 49.给定的字符串中找出小于97的字符存入数组中并输出
- 杯具,万达电商又换CEO
- Wireshark抓包介绍和TCP三次握手分析
- memcache读书笔记(二)
- Problem D: [NOIP2008]传纸条 T3
- c prime plus 得内存,储存类,链接的后记1static
- JAVA_OPTS