IOS开发之UIColectionView

      UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类。

     使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。

     UICollectionView 是一种流式布局界面  是在UITableView 的基础上延伸的一种布局 具体延伸在可以纵向的去操作cell,以及引用了UIcollectionLayout的子类去实现需要的效果

 UIcollectionLayout:是系统提供一种流式布局样式,是一种基类  通常使用它的子类UICollectionFlowLayout

    步骤:1.创建流式布局

         2.创建UICollectionView

         3.挂上代理  实现代理

            3.1、 UICollectionViewDataSource(是对collection 中组的布局操作包括大小,组距,多少组,组宽)

            3.2、 UICollectionViewDelegate  (是针对组之间每个cell的响应事件的代理方法,包括点击,取消选中状态,等等)

            3.3、 UICollectionViewDelegateFlowLayout 是针对界面纵向的操作布局,实现每个小cell之间的设计

        4.自定义UICollectionView

 */

/*

   1.创建cell以及header,footer

   使用代码创建

   - registerClass:forCellWithReuseIdentifier:

   - registerClass:forSupplementaryViewOfKind:withReuseIdentifier:

  

   使用xib创建

   - registerNib:forCellWithReuseIdentifier:

   - registerNib:forSupplementaryViewOfKind:withReuseIdentifier:

   

   复用cell

   - dequeueReusableCellWithReuseIdentifier:forIndexPath:

   - dequeueReusableSupplementaryViewOfKind:

      :withReuseIdentifier:forIndexPath:

      

   2.获取Collection View中的Item及位置

   - indexPathForItemAtPoint:

   - indexPathsForVisibleItems

   - indexPathForCell:

   - cellForItemAtIndexPath:

  */

- (void)viewDidLoad {

    [super viewDidLoad];

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

    UICollectionView *collectionview = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];

    

    collectionview.backgroundColor  =[UIColor orangeColor];

    collectionview.delegate = self;

    collectionview.dataSource = self;

//    注册cell

    [collectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    collectionview.allowsMultipleSelection = YES;//默认为NO,是否可以多选

    [self.view addSubview:collectionview];

    

    

    

}

#pragma marks UICollectionViewDataSource(对cell的操作)=================

//@required

//组里的item个数

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

    return 2;

}

//创建cell

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

    static NSString *cellID = @"cell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath ];

    cell.backgroundColor = [UIColor colorWithRed:(10*indexPath.row)/255.0 green:(20*indexPath.row)/255.0 blue:(30*indexPath.row)/255.0 alpha:1.0f];

    return cell;

    

}

//@optional

//定义展示的section的个数

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

    return 2;

}

//视图上另外补充的footer和header

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

//    

//    NSString *reuseIdentifier;

//    if ([kind isEqualToString: UICollectionElementKindSectionFooter ]){

//        reuseIdentifier = kfooterIdentifier;

//    }else{

//        reuseIdentifier = kheaderIdentifier;

//    }

//    

//    UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];

//    

//    UILabel *label = (UILabel *)[view viewWithTag:1];

//    if ([kind isEqualToString:UICollectionElementKindSectionHeader]){

//        label.text = [NSString stringWithFormat:@"这是header:%ld",(long)indexPath.section];

//    }

//    else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){

//        view.backgroundColor = [UIColor lightGrayColor];

//        label.text = [NSString stringWithFormat:@"这是footer:%ld",(long)indexPath.section];

//    }

//    return view;

//}

//能否移动cell

- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{

    return YES;

}

//移动元素到达哪个位置

- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath{

}

#pragma marks UICollectionViewDelegate(响应事件)=================

/*

//  1.处理选择的Cells

- collectionView:shouldSelectItemAtIndexPath:

- collectionView:didSelectItemAtIndexPath:

- collectionView:shouldDeselectItemAtIndexPath:

- collectionView:didDeselectItemAtIndexPath:

  

//  2.处理Cells的高亮

- collectionView:shouldHighlightItemAtIndexPath:

- collectionView:didHighlightItemAtIndexPath:

- collectionView:didUnhighlightItemAtIndexPath:

*/

//关于cell的状态也有了更为全面的描述 包括选中,高亮,取消选中和高亮

#pragma marks 其他关于设置的方法=================

//定义每个UICollectionViewCell 的大小

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

    return CGSizeMake(50, 50);

}

//定义每个section的margin(边框)

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(5, 5, 5, 5);

}

//每个section中不同的行之间的行间距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

{

    return 10;

}

//每个item之间的间距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

{

    return 100;

}

//选择了某个cell

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

{

    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

//临时改变的颜色   并不是永久改变的   如果需要永久改变需要改变数据源 

    [cell setBackgroundColor:[UIColor greenColor]];

    NSLog(@"=============%ld",(long)indexPath.item);

    NSLog(@"=============%ld",(long)indexPath.row);

    NSLog(@"=============%ld",(long)indexPath.section);

}

//取消选择了某个cell

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

{

    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

    [cell setBackgroundColor:[UIColor redColor]];

}

原文地址:https://www.cnblogs.com/Biaoac/p/5301865.html