常用第三方库:
pod "AFNetworking", “3.0.1”
pod 'MJExtension', '~> 3.0.10' 字典模型
pod 'Masonry', '~> 1.0.0'
pod 'SDWebImage', '~> 3.7.5'
pod 'IQKeyboardManager' 键盘
pod 'TZImagePickerController' 相册、拍照
DOPDropDownMenu - Enhanced 下拉菜单
SDAutoLayout 自适应高度宽度
SDCycleScrollView 轮播
SDRefeshView 上拉刷新
LZCartViewController 购物车
涉及知识点:table表偏移量,购物车利用block回传数值,个人资料出生日期选择,选择相册
#import "HomeViewController.h"
#import "SDCycleScrollView.h"
#import "HomeModels.h"
#import "HomeCell.h"
@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource>{
NSArray *photoArray;
SDRefreshFooterView *_refreshFooter;
}
@property (nonatomic,strong)NSArray *dataArray;
@property(nonatomic,retain)UITableView *tableView;
@property (nonatomic,assign)NSInteger rowCount;
@end
@implementation HomeViewController
#pragma mark - 加载数据
- (NSArray *)dataArray{
if (_dataArray ==nil) {
//路径
NSString *path = [[NSBundlemainBundle]pathForResource:@"Books"ofType:@"plist"];
//读取
NSArray *tempArray = [NSArrayarrayWithContentsOfFile:path];
//可变
NSMutableArray *mutableArray = [NSMutableArrayarray];
NSLog(@"%@",mutableArray);
//转换
for (NSDictionary *dictin tempArray) {
HomeModels *homeModels = [HomeModelsmj_objectWithKeyValues:dict];
[mutableArray addObject:homeModels];
NSLog(@"homeModels = %@",homeModels);
}
//赋值
_dataArray = mutableArray;
}
return_dataArray;
}
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorwhiteColor];
self.tableView = [[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];
[self.viewaddSubview:self.tableView];
UIView *headView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,kMainScreenWidth,kMainScreenWidth+kMainScreenHeight/3)];
self.tableView.tableHeaderView = headView;
// 本地加载 ---创建不带标题的图片轮播器
NSArray *imageArray =@[@"DSC_2709.JPG",@"DSC_2792.JPG"];
SDCycleScrollView *cycleScrollView = [SDCycleScrollViewcycleScrollViewWithFrame:CGRectMake(0,0,kMainScreenWidth,kMainScreenHeight/3)shouldInfiniteLoop:YESimageNamesGroup:imageArray];
// cycleScrollView.delegate = self;
// cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
[headView addSubview:cycleScrollView];
//--- 轮播时间间隔,默认1.0秒,可自定义
//cycleScrollView.autoScrollTimeInterval = 4.0;
//4张图片按钮
NSArray *btnPhotos =@[@"DSC_2749.JPG",@"DSC_2784.JPG",@"DSC_2794.JPG",@"DSC_2712.JPG"];
for (int i =0; i<4; i++) {
float x = (i%2)*(kMainScreenWidth/2);
float y =CGRectGetMaxY(cycleScrollView.frame)+(i/2)*(kMainScreenWidth/2);
UIButton *imageBtn = [[UIButtonalloc]initWithFrame:CGRectMake(x, y,kMainScreenWidth/2,kMainScreenWidth/2)];
[imageBtn setImage:[UIImageimageNamed:btnPhotos[i]]forState:UIControlStateNormal];
// imageBtn.backgroundColor = [UIColor orangeColor];
imageBtn.tag = i;
[imageBtn addTarget:selfaction:@selector(getImageBtn:)forControlEvents:UIControlEventTouchUpInside];
[headView addSubview:imageBtn];
}
self.tableView.delegate =self;
self.tableView.dataSource =self;
// [self creatModelsWithCount:18];
_rowCount = (long)6;
[self.tableViewregisterClass:[HomeCellclass]forCellReuseIdentifier:NSStringFromClass([HomeCellclass])];
__weaktypeof(self) weakSelf =self;
// 上拉加载
_refreshFooter = [SDRefreshFooterViewrefreshView];
[_refreshFooteraddToScrollView:self.tableView];
__weaktypeof(_refreshFooter) weakRefreshFooter =_refreshFooter;
_refreshFooter.beginRefreshingOperation = ^() {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 *NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
// [weakSelf creatModelsWithCount:10];
weakSelf.rowCount +=6;
[weakSelf.tableViewreloadData];
[weakRefreshFooter endRefreshing];
});
};
[self.tableViewreloadData];
}
-(void)getImageBtn:(UIButton *)imageBtn{
}
- (void)cycleScrollView{
// 本地加载 ---创建不带标题的图片轮播器
NSArray *imageArray =@[@"DSC_2709.JPG",@"DSC_2749.JPG",@"DSC_2792.JPG"];
SDCycleScrollView *cycleScrollView = [SDCycleScrollViewcycleScrollViewWithFrame:CGRectMake(0,0,kMainScreenWidth,kMainScreenHeight/3)shouldInfiniteLoop:YESimageNamesGroup:imageArray];
// cycleScrollView.delegate = self;
cycleScrollView.pageControlStyle =SDCycleScrollViewPageContolStyleAnimated;
[self.viewaddSubview:cycleScrollView];
// [self.view addSubview:cycleScrollView];
// cycleScrollView.scrollDirection = UICollectionViewScrollDirectionVertical;
// --- 轮播时间间隔,默认1.0秒,可自定义
//cycleScrollView.autoScrollTimeInterval = 4.0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
return1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
returnself.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Class currentClass = [HomeCellclass];
HomeCell *cell =nil;
HomeModels *model =_dataArray[indexPath.row];
cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(currentClass)];
cell.model = model;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Class currentClass = [HomeCellclass];
HomeModels *model =_dataArray[indexPath.row];
/*
普通版也可实现一步设置搞定高度自适应,不再推荐使用此套方法,具体参看“UITableView+SDAutoTableViewCellHeight”头文件
return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:currentClass];
*/
// 推荐使用此普通简化版方法(一步设置搞定高度自适应,性能好,易用性好)
return [self.tableViewcellHeightForIndexPath:indexPathmodel:modelkeyPath:@"model"cellClass:currentClasscontentViewWidth:[selfcellContentViewWith]];
}
- (CGFloat)cellContentViewWith
{
CGFloat width = [UIScreenmainScreen].bounds.size.width;
// 适配ios7横屏
if ([UIApplicationsharedApplication].statusBarOrientation !=UIInterfaceOrientationPortrait && [[UIDevicecurrentDevice].systemVersionfloatValue] < 8) {
width = [UIScreenmainScreen].bounds.size.height;
}
return width;
}
#import "HomeCell.h"
#import "HomeModels.h"
@implementation HomeCell
{
UILabel *_titleLabel;
UIImageView *_imageView;
UILabel *_price;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [superinitWithStyle:stylereuseIdentifier:reuseIdentifier]) {
[selfsetup];
}
returnself;
}
- (void)setup
{
_titleLabel = [UILabelnew];
[self.contentViewaddSubview:_titleLabel];
_titleLabel.textColor = [UIColordarkGrayColor];
_titleLabel.font = [UIFontsystemFontOfSize:15];
_titleLabel.numberOfLines =0;
_imageView = [UIImageViewnew];
[self.contentViewaddSubview:_imageView];
_imageView.layer.borderColor = [UIColorgrayColor].CGColor;
_imageView.layer.borderWidth =1;
_price = [UILabelnew];
[self.contentViewaddSubview:_price];
CGFloat margin =15;
UIView *contentView =self.contentView;
_imageView.sd_layout
.topSpaceToView(contentView, margin)//距离顶
.leftSpaceToView(contentView, margin)//