【IOS学习笔记】为UICollectionView设置自适应屏幕宽度以及点击效果
2015-12-27 18:28
991 查看
1、设置代理
2、实现方法
笔者使用了一行3个,所以在计算宽度时除了3;间距是2所以3个子试图一共2个间距减去了4。
*注意:由于sizeForItemAtIndexPath是UICollectionViewDelegateFlowLayout的实现方法,但是
UICollectionViewDelegateFlowLayout协议的父级协议是UICollectionViewDelegate
所以可以直接实现sizeForItemAtIndexPath方法
3、RGB为宏定义
@property (weak, nonatomic) IBOutlet UICollectionView *gridview;
_gridview.dataSource=self; _gridview.delegate=self;
2、实现方法
笔者使用了一行3个,所以在计算宽度时除了3;间距是2所以3个子试图一共2个间距减去了4。
*注意:由于sizeForItemAtIndexPath是UICollectionViewDelegateFlowLayout的实现方法,但是
UICollectionViewDelegateFlowLayout协议的父级协议是UICollectionViewDelegate
所以可以直接实现sizeForItemAtIndexPath方法
//设置大小 -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ float width=(self.view.bounds.size.width-4)/3; return CGSizeMake(width, width); } //设置行间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ return 2.0f; } //触发点击事件 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ } //设置允许高亮 - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{ return YES; } //点击结束 - (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell=[colView cellForItemAtIndexPath:indexPath]; cell.backgroundColor=[UIColor whiteColor]; } //点击中 - (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(nonnull NSIndexPath *)indexPath{ UICollectionViewCell *cell=[colView cellForItemAtIndexPath:indexPath]; // cell.backgroundColor=RGB(226, 226, 226); [cell setBackgroundColor:RGB(226, 226, 226)]; }
3、RGB为宏定义
#define RGB(r, g, b) [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0]
相关文章推荐
- UICollectionView \ UICollectionViewController
- Android 应用程序窗口显示状态操作(requestWindowFeature()的应用)
- WPF,布局,Menu,MenuItem,DockPanel,Grid,DockPanel.Dock='',ToolBar,Content,Image,Uri
- Android GUI 之编辑框(EditText)与监视器
- GUI(概述和布局)
- 34.UIImage — 把多张图片合成一张图
- ios uicollectionview
- 33.使用UIGraphicsGetCurrentContext()重绘图像
- 32.利用UIViewAnimation动画制作一个简单的下拉菜单
- 轻松搞定 easyui datagrid 二次加载的问题
- Quicksum
- Handler,MessageQueue,Runnable与Looper
- 24.利用UITableView制作一个的动态的个人信息列表
- iOS UIPageViewController - 使用总结
- requestCode与resultCode不等以及编程中遇到的其它问题
- 【杭电oj】1242-Rescue(bfs,优先队列)
- 22.UITableViewCell根据Cell的内容自适应
- [Android][Build Problems] failed to find build tools revision & failed to find target android 等问题解决
- Servlet之request与response
- 天地图导入AndroidStuido中