UICollectionView的使用及其代理(一)

UICollectionView和UItableView一样是很常用的控件。下面就简单的介绍一下这个控件的一下使用方法。

UICollectionView的使用及其代理(一)_第1张图片
图不重要看代码.jpg
#import "ViewController.h"
#import "OneCollectionViewCell.h"
#import "headerViewReusableView.h"
@interface ViewController ()

@property (strong, nonatomic)UICollectionView *collectionView;

@property (nonatomic,retain)NSDictionary *itemDic; // 承载一个item上面显示的图片和文字
@property (nonatomic,retain)NSMutableArray *allDataArray; // 成方所有的item上显示的内容

@end

static NSString *headerID = @"headerID";
static NSString *footerID = @"footerID";
@implementation ViewController
-(NSMutableArray *)allDataArray{
    if (!_allDataArray) {
        _allDataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _allDataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i < 9; i++) {
        
        NSDictionary *itemDic = [[NSDictionary alloc] initWithObjectsAndKeys:
                                 [NSString stringWithFormat:@"%d.jpg",i],@"imageName",
                                 [NSString stringWithFormat:@"我是第%d个妹子",i] ,@"textLable",
                                 nil];
        [self.allDataArray addObject:itemDic];
        
    }
    self.title = @"ViewController";
    // 由于初始化集合视图需要布局对象,所以我们才需要初始化布局对象,并且设置它的某些属性
    // 这个是系统提供的布局类,可以布局一些比较规则的布局。
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置每个item的大小,
    flowLayout.itemSize = CGSizeMake(100, 140);//宽高
//    flowLayout.itemSize = CGSizeMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
    // 设置列的最小间距,cell之间的距离
    flowLayout.minimumInteritemSpacing = 10;
    // 设置最小行间距
    flowLayout.minimumLineSpacing = 15;
    // 设置布局的内边距
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    // 滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;//纵向滑动
//    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//横向滑动
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];//UICollectionView占满整个屏幕,UICollectionView位置
    
    // 如果未设置背景颜色是黑色设置背景颜色
    collectionView.backgroundColor = [UIColor whiteColor];
    // 设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
    //    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CELL"];
    [collectionView registerClass:[OneCollectionViewCell class] forCellWithReuseIdentifier:@"CELL"];
    
    [self.view addSubview:collectionView];
    
}

//设置某个item是否可以被移动,返回NO则不能移动
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0){
    if (indexPath.row == 2) {
        return YES;
    }else{
        return NO;
    }
}

//移动item的时候,会调用这个方法
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
{
    
}

// 返回分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 2;
}

// 每个分区多少个item
- (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _allDataArray.count;
}
//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    OneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    // 取出每个item所需要的数据
    NSDictionary *dic = [_allDataArray objectAtIndex:indexPath.item];
    // 取出图片名称
    NSString *imageString = [dic objectForKey:@"imageName"];
    cell.imageView.image = [UIImage imageNamed:imageString];
    // 取出文字
    NSString *textString = [dic objectForKey:@"textLable"];
    cell.titleLabel.text = textString;
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/256.0 green:arc4random()%256/256.0 blue:arc4random()%256/256.0 alpha:1];
    
    
    return cell;
}
//header或者foot
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        headerViewReusableView *footer = [collectionView  dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerID forIndexPath:indexPath];
        footer.backgroundColor = [UIColor yellowColor];
        footer.text = [NSString stringWithFormat:@"第%ld个分区的footer",indexPath.section];
        return footer;
    }
    
//    if (indexPath.section >0) {
//        headerViewReusableView *header = [collectionView  dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID forIndexPath:indexPath];
//        header.backgroundColor = indexPath.section%2?[[UIColor purpleColor] colorWithAlphaComponent:1] : [[UIColor blueColor] colorWithAlphaComponent:1];
//        header.text = [NSString stringWithFormat:@"第%ld个分区的header",indexPath.section];
//        return header;
//    }
    return nil;
}

//是否允许某个Item的高亮,返回NO,则不能进入高亮状态
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//当item高亮时触发的方法
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"我已经高亮啦");
}

//结束高亮状态时触发的方法
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"我高亮结束啦");
}

//是否可以选中某个Item,返回NO,则不能选中
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//是否可以取消选中某个Item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 2) {
        return YES;
    }else{
        return NO;
    }
}

//取消选中某个Item时触发的方法
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 4) {
        [self select_no];
    }
}
-(void)select_no
{
    NSLog(@"我被取消选择了");
}

//将要加载某个Item时调用的方法
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0)
{
    
}

//将要加载头尾视图时调用的方法
- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0)
{
    
}

//已经展示某个Item时触发的方法
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 7) {
        NSLog(@"第七个CELL被展示了");
    }
}

//已经展示某个头尾视图时触发的方法
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
    
}

//设置是否展示长按菜单
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//设置要展示的菜单选项
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender
{
    return YES;
}

//实现点击菜单按钮后的触发方法,通过测试,只有copy,cut和paste三个方法可以使用
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender
{
    [self cut:sender];
}
-(void)cut:(id)sender
{
    NSLog(@"我点击了菜单啦");
}




//collectionView进行重新布局时调用的方法
//- (nonnull UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout *)toLayout{
//    
//}

// 点击图片的方法 //已经选中某个item时触发的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"我点击了%ld图片!!!",indexPath.item + 1);
    
}

—>以上基本就是所有的代码了dome在此

你可能感兴趣的:(UICollectionView的使用及其代理(一))