![]()
PrefixHeader.pch
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
#import "WatchTableViewCell.h"
#import "watchModel.h"
#import "DetailViewController.h"
#import "watchViewController.h"
#define kWidth self.contentView.bounds.size.width
#define kHeight self.contentView.bounds.size.height
AppDelegate.m
同上
watchModel.h
@interface watchModel : NSObject
@property(nonatomic,strong)NSString * id;
@property(nonatomic,strong)NSString * title;
@property(nonatomic,strong)NSString * description;
@property(nonatomic,strong)NSString * image_name;
@property(nonatomic,strong)NSString * price;
watchModel.m
@implementation watchModel
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
@end
WatchTableViewCell.h
@interface WatchTableViewCell : UITableViewCell
@property(nonatomic,strong)UILabel * details_label;
@property(nonatomic,strong)UILabel * watch_money;
@property(nonatomic,strong)UIImageView * watch_image;
@end
WatchTableViewCell.m
@implementation WatchTableViewCell
- (UIImageView *)watch_image
{
if (!_watch_image) {
_watch_image =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth / 4, kHeight)];
[self.contentView addSubview:_watch_image];
}
return _watch_image;
}
- (UILabel *)details_label
{
if (_details_label == nil) {
_details_label =[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_watch_image.frame)+10, CGRectGetMinY(_watch_image.frame)+10, kWidth/2, kHeight - 20)];
[self.contentView addSubview:_details_label];
}
return _details_label;
}
- (UILabel *)watch_money
{
if (_watch_money == nil) {
_watch_money =[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_details_label.frame), 0, (kWidth-30)/4, kHeight)];
[self.contentView addSubview:_watch_money];
}
return _watch_money;
}
watchViewController.h
@interface watchViewController : UIViewController
@property(nonatomic,strong)UITableView * tableView;
@end
watchViewController.m
#import "watchViewController.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface watchViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
@property(nonatomic,retain)UIScrollView * scrollView;
@property(nonatomic,retain)UIPageControl * page;
@property(nonatomic,retain)NSTimer * time;
@property(nonatomic,strong)NSMutableArray * dataArray;
@property(nonatomic,strong)NSMutableArray * sectionArray;
@end
@implementation watchViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"我去年买了个表";
self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStyleGrouped)];
self.tableView.backgroundColor = [UIColor cyanColor];
self.tableView.dataSource =self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
//轮播
[self scroll];
//数据
[self getdates];
//注册cell
[self.tableView registerClass:[WatchTableViewCell class] forCellReuseIdentifier:@"cell_id"];
}
//数据
-(void)getdates
{
self.sectionArray = [NSMutableArray array];
self.dataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"GoodsList" ofType:@"plist"]];
for (NSDictionary * dic in self.dataArray) {
NSString * sectionStr = [dic objectForKey:@"name"];
[self.sectionArray addObject:sectionStr];
}
}
//轮播
-(void)scroll
{
UIView * view= [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 180)];
self.scrollView.contentSize = CGSizeMake(kScreenWidth * 8, 0);
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
//横向移动
self.scrollView.showsHorizontalScrollIndicator = YES;
[view addSubview:self.scrollView];
//加图片
for (int i=0; i<8; i++)
{
UIScrollView * scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(kScreenWidth * i, 0, kScreenWidth, kScreenHeight)];
[self.scrollView addSubview:scroll];
UIImageView * imv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight/4)];
imv.image = [UIImage imageNamed:[NSString stringWithFormat:@"apple_watch_sport_0%d@2x.jpg",i+1]];
[scroll addSubview:imv];
}
//加page
_page = [[UIPageControl alloc]initWithFrame:CGRectMake(200, 200, 200, 30)];
//选中时颜色
_page.currentPageIndicatorTintColor = [UIColor redColor];
_page.numberOfPages = 8;
_page.currentPage = 0 ;
//未选中时颜色
_page.pageIndicatorTintColor = [UIColor grayColor];
//添加事件
[_page addTarget:self action:@selector(pageAction:) forControlEvents:(UIControlEventValueChanged)];
[view addSubview:_page];
self.scrollView.delegate=self;
for (UIScrollView *s in self.scrollView.subviews) {
s.delegate=self;
}
self.tableView.tableHeaderView = view;
_time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
}
-(void)timeAction
{
static int i = 0;
i++;
if (i>7) {
i=0;
}
[self.scrollView setContentOffset:CGPointMake(kScreenWidth * i, 0)];
_page.currentPage = i;
}
//将scrollView与page关联
-(void)pageAction:(UIPageControl *)sender
{
NSInteger index = sender.currentPage;
_scrollView.contentOffset = CGPointMake(kScreenWidth * index, 0);
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
_page.currentPage = scrollView.contentOffset.x/kScreenWidth;
}
#pragma UItableView的代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.dataArray[section] objectForKey:@"kind"] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
WatchTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_id" forIndexPath:indexPath];
watchModel * model = [[watchModel alloc]init];
[model setValuesForKeysWithDictionary:[self.dataArray[indexPath.section]objectForKey:@"kind"][indexPath.row]];
//这种方法不太懂
//(一)==========================================================
// NSString *image =[model.image_name stringByReplacingOccurrencesOfString:@".jpg" withString:@"@2x.jpg"];
// cell.watch_image.image =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:image ofType:nil]];
// cell.details_label.text = model.title;
// cell.details_label.numberOfLines = 0;
// cell.watch_money.text = model.price;
// cell.watch_money.font =[UIFont systemFontOfSize:15];
//==============================================================
//(二)
cell.watch_image.image = [UIImage imageNamed:model.image_name];
cell.details_label.text = model.title;
cell.watch_money.text = model.price;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController * dataVC = [DetailViewController new];
watchModel * model = [watchModel new];
[model setValuesForKeysWithDictionary:[self.dataArray[indexPath.section]objectForKey:@"kind"][indexPath.row]];
dataVC.title_String = model.title;
dataVC.image_String = model.image_name;
[self.navigationController pushViewController:dataVC animated:YES];
}
//返回的是分区头标题,从plist文件中提取出来的,就是三种手表类型的名字.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return self.sectionArray[section];
}