那么今天给同学们带来一个团购的小demo,主要包括图片轮播器,自定义cell,以及刷新表格,其中涉及很多基础和细节的东西,那么废话不多说,先看效果图,直接上代码!
//
// ZZTg.h
// 07-团购
//
// Created by 周昭 on 16/11/22.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ZZTg : NSObject
/**
* 标题
*/
@property (nonatomic,copy) NSString *title;
/**
* 价格
*/
@property (nonatomic,copy) NSString *price;
/**
* 图片
*/
@property (nonatomic,copy) NSString *icon;
/**
* 购买人数
*/
@property (nonatomic,copy) NSString *buyCount;
+ (instancetype)tgWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
//
// ZZTg.m
// 07-团购
//
// Created by 周昭 on 16/11/22.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import "ZZTg.h"
@implementation ZZTg
+ (instancetype)tgWithDict:(NSDictionary *)dict
{
return [[selfalloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [superinit]) {
#pragma mark - kvc 直接字典转模型
[selfsetValuesForKeysWithDictionary:dict];
}
returnself;
}
@end
//
// ZZTgHeader.h
// 07-团购
//
// Created by 周昭 on 16/11/22.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ZZTgHeader : NSObject
/**
* 图标
*/
@property (nonatomic,copy) NSString *icon;
+ (instancetype)headerWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
//
// ZZTgHeader.m
// 07-团购
//
// Created by 周昭 on 16/11/22.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import "ZZTgHeader.h"
@implementation ZZTgHeader
+ (instancetype)headerWithDict:(NSDictionary *)dict
{
return [[selfalloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [superinit]) {
[selfsetValuesForKeysWithDictionary:dict];
}
returnself;
}
@end
//
// ZZTgCell.h
// 07-团购
//
// Created by 周昭 on 16/11/22.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import <UIKit/UIKit.h>
@class ZZTg;
@interface ZZTgCell :UITableViewCell
/**
* 团购模型
*/
@property (nonatomic,strong) ZZTg *tg;
/**
* 通过一个tableView来创建一个cell
*/
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end
//
// ZZTgCell.m
// 07-团购
//
// Created by 周昭 on 16/11/22.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import "ZZTgCell.h"
#import "ZZTg.h"
@interface ZZTgCell()
/**
* 图标
*/
@property (nonatomic,weak) UIImageView *iconImg;
/**
* 标题
*/
@property (nonatomic,weak) UILabel *titleLabel;
/**
* 价格
*/
@property (nonatomic,weak) UILabel *priceLabel;
/**
* 购买数
*/
@property (nonatomic,weak) UILabel *buyLabel;
@end
@implementation ZZTgCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
staticNSString *ID = @"tg";
ZZTgCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID];
if (cell ==nil) {
cell = [[ZZTgCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:ID];
}
return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];
UIImageView *iconImg = [[UIImageViewalloc] init];
[self.contentViewaddSubview:iconImg];
self.iconImg = iconImg;
UILabel *titleLabel = [[UILabelalloc] init];
titleLabel.font = [UIFontboldSystemFontOfSize:16.0f];
[self.contentViewaddSubview:titleLabel];
self.titleLabel = titleLabel;
UILabel *priceLabel = [[UILabelalloc] init];
priceLabel.font = [UIFontsystemFontOfSize:15.5f];
priceLabel.textColor = [UIColorcolorWithRed:239 /255.0 green:134 /255.0 blue:130 /255.0 alpha:1.0];
[self.contentViewaddSubview:priceLabel];
self.priceLabel = priceLabel;
UILabel *buyLabel = [[UILabelalloc] init];
buyLabel.font = [UIFontsystemFontOfSize:14.0f];
buyLabel.textColor = [UIColorlightGrayColor];
buyLabel.textAlignment =NSTextAlignmentRight;
[self.contentViewaddSubview:buyLabel];
self.buyLabel = buyLabel;
returnself;
}
/**
* 拿到真实的尺寸进行布局
*/
- (void)layoutSubviews
{
[superlayoutSubviews];