#import "WJ_VoteViewController.h"
#import "WJ_VoteCollectionViewCell.h"
#import "Masonry.h"
@interface WJ_VoteViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
UICollectionView *mainCollectionView;
}
@property(nonatomic, assign) CGFloat itemWidth;
@end
@implementation WJ_VoteViewController
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:NO animated:animated];
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.itemWidth = (IPHONE_WIDTH - 5 * 10) / 4;
[self setNavUI];
[self setBottomUI];
}
//設(shè)置導(dǎo)航欄
-(void)setNavUI
{
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backItem];
self.title = @"下周菜單投票";
}
//導(dǎo)航欄下面的UI
-(void)setBottomUI
{
self.view.backgroundColor = [UIColor whiteColor];
//1.初始化layout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//設(shè)置collectionView滾動(dòng)方向
[layout setScrollDirection:UICollectionViewScrollDirectionVertical];
//設(shè)置headerView的尺寸大小
layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 100);
//2.初始化collectionView
mainCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
[self.view addSubview:mainCollectionView];
//大view的背景圖
mainCollectionView.backgroundColor = [UIColor clearColor];
mainCollectionView.bounces = YES;
//3.注冊(cè)collectionViewCell
//注意霉颠,此處的ReuseIdentifier 必須和 cellForItemAtIndexPath 方法中 一致 均為 cellId
[mainCollectionView registerClass:[WJ_VoteCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
//注冊(cè)headerView 此處的ReuseIdentifier 必須和 cellForItemAtIndexPath 方法中 一致 均為reusableView
[mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
//4.設(shè)置代理
mainCollectionView.delegate = self;
mainCollectionView.dataSource = self;
}
#pragma mark collectionView代理方法
//返回section個(gè)數(shù)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
//每個(gè)section的item個(gè)數(shù)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 12;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
WJ_VoteCollectionViewCell *cell = (WJ_VoteCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
// cell.botlabel.text = [NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.section,(long)indexPath.row];
cell.backgroundColor = [UIColor blueColor];
return cell;
}
//設(shè)置每個(gè)item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(self.itemWidth, 94);
}
//footer的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
return CGSizeMake(10, 10);
}
//header的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(10, 10);
}
//設(shè)置每個(gè)item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(10, 10, 10, 10);
}
//設(shè)置每個(gè)item水平間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 10;
}
//設(shè)置每個(gè)item垂直間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 5;
}
//點(diǎn)擊item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
WJ_VoteCollectionViewCell *cell = (WJ_VoteCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
// NSString *msg = cell.botlabel.text;
// NSLog(@"%@",msg);
}
#pragma mark - 監(jiān)聽自定導(dǎo)航欄按鈕事件
-(void)navBtnClick:(UIButton *)btn{
[self.navigationController popViewControllerAnimated:YES];
}
collectionView點(diǎn)擊bounces.gif
從運(yùn)行效果上看垂直方向并不能滾動(dòng),快快想個(gè)解決辦法呀荆虱!
還好大金哥幫我想了一個(gè)辦法
mainCollectionView.bounces = YES;換成這句
mainCollectionView.alwaysBounceVertical = YES;
效果出來如圖所示:
collectionView點(diǎn)擊bounces1.gif