iOS UICollectionView小结 + 选中效果
来源:互联网 发布:淘宝网卖情趣用品 编辑:程序博客网 时间:2024/05/24 06:30
UICollectionView的出现使得复杂的界面简单化,下面带来UICollectionView的一些使用细节,以及多选的效果
#import "ViewController.h"
#import "CollectionReusableView.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//flowLayout 控制UICollectionView布局
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayoutalloc]init];
//section内置大小
flowLayout.sectionInset = UIEdgeInsetsMake(10, 25, 10, 25);
//默认最小为10如果比10小需要重新赋值
// flowLayout.minimumInteritemSpacing = 5;
//行间距
flowLayout.minimumLineSpacing = 5;
//item大小如果只有一种size 不适用代理方法返回大小
// flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 30)/ 3, 200);
//预估算,节省时间
//多种item大小使用estimatedItemSize提高代码运算效率,提高流畅度
//flowLayout.estimatedItemSize = CGSizeMake(((SCREEN_WIDTH - 20) + (SCREEN_WIDTH - 30)/3)/2, 200);
//设置header大小
flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 50);
// flowLayout.sectionHeadersPinToVisibleBounds = YES;
// flowLayout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 50);
UICollectionView *collection = [[UICollectionViewalloc]initWithFrame:self.view.framecollectionViewLayout:flowLayout];
//设置允许多选
collection.allowsMultipleSelection =YES;
//签代理
collection.delegate = self;
collection.dataSource = self;
collection.backgroundColor = [UIColorwhiteColor];
//注册
[collection registerClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:@"UICollectionViewCell"];
[self.viewaddSubview:collection];
//头部区域底部区域 同cell一样遵循重用机制
[collection registerClass:[CollectionReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"UICollectionReusableView"];
[collection registerClass:[UICollectionReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"UICollectionReusable"];
}
#pragma mark - collection delegate/dataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (section == 0) {
return 1;
}else if (section ==1){
return 4;
}else{
return 5;
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell"forIndexPath:indexPath];
if (cell.isSelected) {
cell.backgroundColor = [UIColorgrayColor];
}else{
cell.backgroundColor = [UIColororangeColor];
}
return cell;
}
#pragma mark -
//collView学名是集合视图 tableView是表视图
//collView中的foot header是曽补视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if (kind ==UICollectionElementKindSectionHeader ) {
CollectionReusableView *reusableView = [collectionViewdequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusableView"forIndexPath:indexPath];
reusableView.backgroundColor = [UIColorredColor];
return reusableView;
}else{
UICollectionReusableView *reusable = [collectionViewdequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusable"forIndexPath:indexPath];
reusable.backgroundColor = [UIColorblackColor];
return reusable;
}
}
//判断分区,可以直接签代理或者进入flowlayout里直接复制方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section ==0) {
NSLog(@"1111111");
return CGSizeMake(375,200);
}else if (indexPath.section ==1){
return CGSizeMake(SCREEN_WIDTH /2 - 50 , 100);
}else{
return CGSizeMake(SCREEN_WIDTH -20 , 200);
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//根据idenxPath获取对应的cell
UICollectionViewCell *cell = [collectionViewcellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColorgrayColor];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionViewcellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColororangeColor];
}
#import "CollectionReusableView.h"
@interface CollectionReusableView ()
@property (nonatomic ,retain)UILabel *label;
@end
@implementation CollectionReusableView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [superinitWithFrame:frame]) {
self.label = [[UILabelalloc]initWithFrame:CGRectMake(0,0, frame.size.width, frame.size.height)];
self.label.text =@"Amydom";
self.label.textColor = [UIColorlightGrayColor];
self.label.font = [UIFontsystemFontOfSize:20];
[self addSubview:self.label];
}
return self;
}
@end
- iOS UICollectionView小结 + 选中效果
- iOS uicollectionview滚动选中状态错乱
- iOS 中UICollectionView中的选中和取消选中
- IOS-使用UiTableview模仿UICollectionView的效果
- (2)iOS用UICollectionView实现Gallery效果
- iOS开发UICollectionView瀑布流效果
- iOS 实现UICollectionView头部悬停效果
- UICollectionView小结
- iOS UITableView 和 UICollectionView 设置默认选中状态
- iOS UICollectionView 卡片效果 传送带效果 Carousel FlowLayout
- [iOS高级] UICollectionView实现瀑布流效果
- [iOS高级] UICollectionView实现瀑布流效果
- [iOS高级] UICollectionView实现瀑布流效果
- iOS开发之UICollectionView之悬浮效果实现
- IOS UICollectionView
- iOS- UICollectionView
- iOS UICollectionView
- iOS UICollectionView
- 修改表_模式
- 截屏/截取屏幕指定区域
- ReactNative 第四节 实例开发
- C++对象的内存分布和虚函数表
- VC父窗口和子窗口
- iOS UICollectionView小结 + 选中效果
- JAVA——高级for循环
- 线段相交之精粹和交点问题
- 删除360天擎(企业版)---不知道密码情况下
- 有关zipalign的一些问题
- android中Activity中的WindowManager与Window
- 解决更改JAVA_HOME后jdk版本没有更改问题
- [unity]在unity中创建圆锥体
- docker相关文章