阅读更多
//
// ViewController.m
// BookStore
//
// Created by cyd on 14-11-21.
// Copyright (c) 2014年 cyd. All rights reserved.
//
#import "IndexViewController.h"
#import "UIViewController+MMDrawerController.h"
@interface IndexViewController ()
@end
@implementation IndexViewController
int flag=0;
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title=@"图书列表";
// self.view.backgroundCo lor=[UIColor whiteColor];
[self creatCollectionView];
[self creatTableView];
[self setupLeftMenuButton];
[self setupRightBarButon];
// SaveDataModel *saveModel=[[SaveDataModel alloc]init];
// [saveModel writeIntoLoaldata];
[self tableViewInitShow];
[self getLocalData];
}
-(void)tableViewInitShow
{
_refreshHeaderTableView = [self refresh];
_refreshHeaderCollectionView = [self refresh];
[m_tableView addSubview:_refreshHeaderTableView];
}
-(EGORefreshTableHeaderView *)refresh
{
EGORefreshTableHeaderView *egoView=[[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.view.bounds.size.height, self.view.frame.size.width, self.view.bounds.size.height)];
egoView.delegate=self;
[egoView refreshLastUpdatedDate];
return egoView;
}
-(void)reloadTableViewDataSource
{
_reLoading=YES;
}
-(void)doneLoadingTableViewData
{
_reLoading = NO;
if (flag%2==0)
{
[_refreshHeaderTableView egoRefreshScrollViewDataSourceDidFinishedLoading:m_tableView];
[m_tableView reloadData];
}
else{
[_refreshHeaderCollectionView egoRefreshScrollViewDataSourceDidFinishedLoading:m_collectionView];
[m_collectionView reloadData];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (flag%2==0)
[_refreshHeaderTableView egoRefreshScrollViewDidScroll:scrollView];
else{
[_refreshHeaderCollectionView egoRefreshScrollViewDidScroll:scrollView];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (flag%2==0)
[_refreshHeaderTableView egoRefreshScrollViewDidEndDragging:scrollView];
else{
[_refreshHeaderCollectionView egoRefreshScrollViewDidEndDragging:scrollView];
}
}
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view
{
[self reloadTableViewDataSource];
[self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3.0];
}
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view
{
return _reLoading;
}
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view
{
return [NSDate date];
}
/////////////////////////////////////////////////////
-(UICollectionView *)creatCollectionView{
UICollectionViewFlowLayout *flowRight = [[UICollectionViewFlowLayout alloc] init];
m_collectionView= [[UICollectionView alloc]initWithFrame:CGRectMake(0, 60,self.view.frame.size.width,self.view.frame.size.height) collectionViewLayout:flowRight];
m_collectionView.dataSource=self;
m_collectionView.delegate=self;
m_collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:m_collectionView];
return m_collectionView;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return listData.count;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString * CellIdentifier =[NSString stringWithFormat:@"GradientCell%ld",indexPath.row];
[m_collectionView registerClass:[UICollectionViewCell class ]forCellWithReuseIdentifier:CellIdentifier];
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *group=[[NSDictionary alloc] initWithObjectsAndKeys:indexPath,@"indexpath",cell,@"cell",nil];
[NSThread detachNewThreadSelector:@selector(loadImageCollectionView:) toTarget:self withObject:group];
[self creatLabelPicture:cell :indexPath :CGRectMake(5, 105, 70, 30) :@"title"];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(96, 100);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 20, 20, 20);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
BookInfoViewController *bookInfo=[[BookInfoViewController alloc]init:[listData objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:bookInfo animated:true];
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 50;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 2;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-(UITableView *)creatTableView
{
m_tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 60, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height) style:UITableViewStylePlain];
m_tableView.dataSource=self;
m_tableView.delegate=self;
m_tableView.backgroundColor = [UIColor clearColor];
[self.view addSubview:m_tableView];
return m_tableView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identify = [NSString stringWithFormat:@"cell%ld",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
}
NSDictionary *group=[[NSDictionary alloc] initWithObjectsAndKeys:indexPath,@"indexpath",cell,@"cell",nil];
[NSThread detachNewThreadSelector:@selector(loadImageTableView:) toTarget:self withObject:group];
[self creatLabel:cell :indexPath :CGRectMake(105, 3, 200, 20) :@"\t" :@"title"];
[self creatLabel:cell :indexPath :CGRectMake(105, 29, 200, 20) :@"价格:":@"price"];
[self creatLabel:cell :indexPath :CGRectMake(105, 49, 200, 20) :@"出版日期:":@"pubdate"];
[self creatLabel:cell :indexPath :CGRectMake(105, 69, 200, 20) :@"出版社:":@"publisher"];
[self creatLabelSpecial:cell :indexPath :CGRectMake(105, 88, 200, 20) :@"作者:":@"author"];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BookInfoViewController *bookInfo=[[BookInfoViewController alloc]init:[listData objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:bookInfo animated:true];
}
///////////////////////////////////////////////////////////////////////////
-(void)creatLabelSpecial
UITableViewCell *)cell
NSIndexPath *)indexPath
CGRect)frame
NSString *)leftHalf
NSString *)forkey
{
UILabel *bookInfo = [[UILabel alloc] initWithFrame:frame];
NSArray *rightHalf=[[listData objectAtIndex:indexPath.row]objectForKey:forkey];
NSString *bookText=[NSString stringWithFormat:@"%@%@",leftHalf,rightHalf[0]];
bookInfo.text=bookText;
[cell addSubview:bookInfo];
}
-(void)creatLabel
UITableViewCell *)cell
NSIndexPath *)indexPath
CGRect)frame
NSString *)leftHalf
NSString *)forkey
{
UILabel *bookInfo = [[UILabel alloc] initWithFrame:frame];
NSString *rightHalf=[[listData objectAtIndex:indexPath.row]objectForKey:forkey];
NSString *bookText=[NSString stringWithFormat:@"%@%@",leftHalf,rightHalf];
bookInfo.text=bookText;
[cell addSubview:bookInfo];
}
////////////////////////////////
-(void)loadImageTableView:(NSDictionary *)group
{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 2, 70, 100)];
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[[[listData objectAtIndex:[[group objectForKey:@"indexpath"] row]] objectForKey:@"images"] objectForKey:@"small"]]];
UIImage *img=[UIImage imageWithData:data];
imageView.image=img;
[[group objectForKey:@"cell"] addSubview:imageView];
}
-(void)creatLabelPicture
UICollectionViewCell *)cell
NSIndexPath *)indexPath
CGRect)frame
NSString *)forkey
{
UILabel *labelPicture = [[UILabel alloc] initWithFrame:frame];
NSString *pictureName=[[listData objectAtIndex:indexPath.row]objectForKey:forkey];
labelPicture.text=pictureName;
[labelPicture setTextColor:[UIColor blackColor]];
labelPicture.font =[UIFont systemFontOfSize:10];
labelPicture.lineBreakMode=NSLineBreakByCharWrapping;
labelPicture.numberOfLines=0;
[cell addSubview:labelPicture];
}
-(void)loadImageCollectionView:(NSDictionary *)group
{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 2, 70, 100)];
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[[[listData objectAtIndex:[[group objectForKey:@"indexpath"] row]] objectForKey:@"images"] objectForKey:@"small"]]];
UIImage *img=[UIImage imageWithData:data];
imageView.image=img;
[[group objectForKey:@"cell"] addSubview:imageView];
}
-(void)getLocalData
{
ReadDataModel *readData=[[ReadDataModel alloc]init];
NSArray *arry=[readData readData];
listData=arry;
NSLog(@"%@",[[[arry objectAtIndex:0]objectForKey:@"images"]objectForKey:@"small"]);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listData count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 120.0;
}
////////////////////////////////////////////////////////
- (void)setupLeftMenuButton
{
MMDrawerBarButtonItem * leftDrawerButton = [[MMDrawerBarButtonItem alloc] initWithTarget:self action:@selector(leftDrawerButtonPress:)];
[self.navigationItem setLeftBarButtonItem:leftDrawerButton];
}
- (void)leftDrawerButtonPress
id)sender
{
[self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
-(void)setupRightBarButon{
UIBarButtonItem *btnSave = [[UIBarButtonItem alloc]
initWithTitle:@"图片浏览"
style:UIBarButtonItemStylePlain
target:self
action:@selector(clickBookList:)];
self.navigationItem.rightBarButtonItem = btnSave;
}
-(void)clickBookList
id)sender
{ if(flag%2==0){
[self creatCollectionView];
//[self refresh];
[m_collectionView addSubview:_refreshHeaderCollectionView];
[self.view bringSubviewToFront:m_collectionView];
}
else
{
[self.view bringSubviewToFront:m_tableView];
}
flag++;
NSLog(@"%d",flag);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end