UICollectionView相对于UITableView有更加自由的布局,做出的界面可变性更大最近开始接触使用UICollectionView,整理了一下常用的代理方法

首先需要先添加UICollectionView的代理:UICollectionViewDelegate   UICollectionViewDataSource  UICollectionViewDelegateFlowLayout

在viewdidLoad的时候注册UICollectionView 的cell

UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(, , self.width, self.height) collectionViewLayout:flowLayout];
[collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"];

需要需要添加headerView或者footerView同样需要注册

[collection registerClass:[collectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];//collectionHeaderView是自定义的view

想要定义错两3c提哦弄View的UI就需要在它的代理方法中进行设置就行

//返回headerView的大小为宽320  高 100
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
CGSize size = CGSizeMake(, ); return size;
}

定义需要显示的headerView UI,因为UICOllectionView的headerView是复用的,所以需要像使用Cell一样在代理方法中自定义

//返回headerView
- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil; if (kind == UICollectionElementKindSectionHeader)
{
VoteTableHeaderView *myHeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
//在这里进行headerView的操作
reusableview = myHeaderView;
} return reusableview;
}
//返回section 的数量
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return ;
}
//返回对应section的item 的数量
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [self.myDataArr count];
}
//创建和复用cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//重用cell
MineVoteTableViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MIneVoteCell" forIndexPath:indexPath];
//赋值给cell return cell;
}
//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(self.view.width/, );
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(, , , );
}
//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return ;
}
//选择了某个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//在这里进行点击cell后的操作
}
//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return ;
}

UICollectionView的简单使用和常用代理方法的更多相关文章

  1. [Swift通天遁地]一、超级工具-(8)地图视图MKMapView的常用代理方法

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. UIScrollView的基本使用和一些常用代理方法

    - (void)viewDidLoad { [super viewDidLoad]; scrollView = [[UIScrollView alloc] initWithFrame:CGRectMa ...

  3. IOS UIScrollView常用代理方法

    iOS UIScrollView代理方法有很多,从头文件中找出来学习一下 //只要滚动了就会触发 - (void)scrollViewDidScroll:(UIScrollView *)scrollV ...

  4. .net学习之多线程、线程死锁、线程通信 生产者消费者模式、委托的简单使用、GDI(图形设计接口)常用的方法

    1.多线程简单使用(1)进程是不执行代码的,执行代码的是线程,一个进程默认有一个线程(2)线程默认情况下都是前台线程,要所有的前台线程退出以后程序才会退出,进程里默认的线程我们叫做主线程或者叫做UI线 ...

  5. UITableView的常用属性和代理方法

    以下是近期总结的关于tableView的一些属性和代理方法,以及一些常见的问题,现汇总如下,今后还会持续更新,请继续关注:   tableView 的头部和尾部视图属性: UISwitch *foot ...

  6. 通读AFN②--AFN的上传和下载功能分析、SessionTask及相应的session代理方法的使用细节

    这一部分主要研究AFN的上传和下载功能,中间涉及到各种NSURLSessionTask的一些创建的解析和HTTPSessionManager对RESTful风格的web应用的支持,同时会穿插一点NSU ...

  7. iPhone页面的常用调试方法

    在iPhone中调试,大体上与上文 安卓中的移动页面调试 类似,区别主要是iOS系统中的一些限制,导致某些工具无法使用. 本文基于此,简要介绍在iPhone中如何调试页面. 最终可以实现在Mac平台使 ...

  8. UICollectionView的简单认识和简单实用

    摘要 UICollectionView是比UITableView更加复杂的UI控件,通过它可以实现许多复杂的流布局.但对我们来说,系统提供的接口十分简单易用,并且有十分强的制定性. iOS流布局UIC ...

  9. iOS 流布局 UICollectionView使用(简单使用)

    简介 UICollectionView是iOS6之后引入的一个新的UI控件,它和UITableView有着诸多的相似之处,其中许多代理方法都十分类似.简单来说,UICollectionView是比UI ...

随机推荐

  1. Tomcat unable to start

    在学习springMvc时,导入springfreemarker 的jar包,写好web.xml,config.xml 后. 部署到tomcat,异常如下: 八月 27, 2016 5:44:41 下 ...

  2. github多人协作

    1.字符串处理(编码原理) git clone git@github.com:lookphp/LaravelCms.git git add . git commit -m "修改的内容-需要 ...

  3. eclipse修改字体

    修改xml字体: window→Preferences→General→Colors and Fonts→Basic→Text Font

  4. 配置Kestrel 网址Urls

    配置Kestrel 网址Urls ASP.NET Core中如何配置Kestrel Urls呢,大家可能都知道使用UseUrls() 方法来配置. 今天给介绍全面的ASP.NET Core 配置 Ur ...

  5. [TYVJ] P1015 公路乘车

    公路乘车 描述 Description 一个特别的单行街道在每公里处有一个汽车站.顾客根据他们乘坐汽车的公里使来付费.例如样例的第一行就是一个费用的单子. 没有一辆车子行驶超过10公里,一个顾客打算行 ...

  6. 三元运算符和GridView数据显示

    三元运算符嵌套使用:<%# Eval("InsertType").ToString() == "0" ? "数据库" : Eval(& ...

  7. shell 中如何判断前一个命令是否执行成功

    shell 中如何判断前一个命令是否执行成功 通过判断返回值来解决: if [ $? -eq 0 ];then 命令正确的分支 else   命令失败的分支 fi

  8. 【转】CTS tests 4.2_r4

    原文网址:http://www.xuebuyuan.com/1722006.html Precondition: 1.Get android sdk 2.Set adb to environment ...

  9. 【转】Android用NDK和整套源码下编译JNI的不同

    原文网址:http://www.devdiv.com/android_ndk_jni_-blog-99-2101.html 前些天要写个jni程序,因为才几行代码,想着用ndk开发可能容易些,就先研究 ...

  10. bzoj1655 [Usaco2006 Jan] Dollar Dayz 奶牛商店

    Description Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of to ...