iOS项目 瀑布流 实现

一直对于collectionview处于半懂不懂的状态

 

所以现在自己做了一个,实现效果如下

 

 

 

 

 

#import "ViewController.h"

#import "CollectionViewCell.h"

 

 

 

static NSString *identifierCell = @"cellID";

 

@interface ViewController ()<UICollectionViewDelegateFlowLayout>

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.collectionView.allowsMultipleSelection = YES;

    self.collectionView.allowsSelection = YES;

    self.collectionView.showsVerticalScrollIndicator = YES;

    self.collectionView.backgroundColor = [UIColor grayColor];

 

    [self registerHeaderAndFooter];

    // Do any additional setup after loading the view, typically from a nib.

}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

 

}

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath

{

    CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];

    cell.selectedBtn.selected = NO;

    [cell.selectedBtn setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];

 

}

 

-(void)registerHeaderAndFooter{

 

    [self.collectionView registerNib:[UINib nibWithNibName:@"HeaderCollectionReusableView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderCollectionReusableView"];

    

    [self.collectionView registerNib:[UINib nibWithNibName:@"FooterCollectionReusableView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterCollectionReusableView"];

 

}

 

 

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

    if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {

        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterCollectionReusableView" forIndexPath:indexPath];

        

        return footer;

    }

    

    

    UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderCollectionReusableView" forIndexPath:indexPath];

    return header;

 

 

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

 

    return CGSizeMake(150, 150);

 

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section

{

 

    return CGSizeMake(80, 80);

}

 

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

{

 

    return CGSizeMake(80, 80);

}

 

 

//-(BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{

//

//    return YES;

//

//

//}

 

 

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

    

    CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];

//    cell.selectedBtn.userInteractionEnabled = NO;

    cell.selectedBtn.selected = YES;

    [cell.selectedBtn setBackgroundImage:[UIImage imageNamed:@"2"] forState:UIControlStateSelected];

 

}

 

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}

 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

 

    return 5;

}

 

 

 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

 

    return 20;

 

}

 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierCell forIndexPath:indexPath];

    

 

    return cell;

 

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

 

 

 

关键一步

1
2
3
4
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height=100+(arc4random()%100);
    return  CGSizeMake(100, height);
}

@end

 

posted @ 2016-03-03 09:55  small-elephant_A-Do  阅读(212)  评论(0编辑  收藏  举报