IOS开发之UIColectionView

摘要:
UICollectionView和UICollectionViewController类是iOS 6为显示集合视图而引入的新API。布局更加灵活,可以实现多列布局。用法类似于UITableView和UITableViewController类。若要使用UICollectionView,必须实现UICollectionViewDataSource、UICollectionViewDelegate、UICol

      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]];

}

免责声明:文章转载自《IOS开发之UIColectionView》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Windows:任务调度器var和let区别简述下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

iOS开发(Swift):创建UINavigationView的三种方法

UINavigationController是iOS开发中很常用的一种组件,由于种种原因许多人喜欢从代码创建视图控件,包括UINavigationController,但是有时候我们的屏幕控件太多,一方面使用storyboard可以方便设计,但是另一方面又需要用代码创建UINavigationController来灵活控制程序运行,下面将分别介绍代码,IB...

C# 文件导出 EXCEL

#region 读excel            //using (Stream stream = File.OpenRead(@"c:\Book1.xls"))            //{            //    //内存中的excel文件            //    HSSFWorkbook workBook = new HSSFW...

iOS开发UI篇—多控制器和导航控制器简单介绍

iOS开发UI篇—多控制器和导航控制器简单介绍 一、多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单。当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个view时,可以用一个大的view去管理1个或者多个小view,控制器也是如此,用1个控制器去管理其他多个控制器 比如,用一个控制器A去管理3个控制器B、C、D...

用python从符合一定格式的txt文档中逐行读取数据并按一定规则写入excel(openpyxl支持Excel 2007 .xlsx格式)

前几天接到一个任务,从gerrit上通过ssh命令获取一些commit相关的数据到文本文档中,随后将这些数据存入Excel中。数据格式如下图所示 观察上图可知,存在文本文档中的数据符合一定的格式,通过python读取、正则表达式处理并写入Excel文档将大大减少人工处理的工作量。   1. 从gerrit获取原始信息,存入文本文档:    $ssh –p...

IOS开发NSString与int和float的相互转换以及字符串拼接、NSString、NSData、char* 类型之间的转换

一、NSString与int和float的相互转换 NSString *tempA = @"123"; NSString *tempB = @"456"; 1.字符串拼接 NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB]; 2.字符转intint intStr...

虚拟机IOS开发环境搭建教程

来源:http://www.cnblogs.com/xiaoyaoju/archive/2013/05/21/3091171.html 安装条件: 硬件:一台拥有支持虚拟技术的64位双核处理器和2GB以上内存的PC。 注意:运行MAC OS,需要电脑支持虚拟技术(VT),安装时,需要将VT启动,在BIOS中开启。 关于如何检测你的电脑支持VT,可以去下面这...