顶部悬浮
layout.sectionHeadersPinToVisibleBounds = YES;
cell右对齐
if (@available(iOS 9.0, *)) {
_collectionView.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
}
cell居左
UICollectionViewCell「居左显示」 - SegmentFault 思否
collectionView居中
for(int i=0;i<_dqArr.count;i++){
NSDictionary *dic = _dqArr[i];
float wid0 = [SocialTool getWid:dic[@"name"] font:[UIFont fontWithName:@"PingFangSC-Regular" size:14]]+40*wzoom+10+20*wzoom;
if (wid + wid0>ScreenWidths-80*wzoom) {
if (wid0>wid) {
wid = wid0;
}
break;
}else{
wid = wid + wid0 ;
}
}
获取cell位置
NSIndexPath *ind = [NSIndexPath indexPathForItem:_systemSetArr.count-1 inSection:0];
UICollectionViewLayoutAttributes *attributes = [_selCollectionView1 layoutAttributesForItemAtIndexPath:ind];
CGPoint cellCenter = attributes.center;
CGPoint anchorPoint = [_selCollectionView1 convertPoint:cellCenter toView:_selCollectionView1
];
防重用
NSString *identifier=[NSString stringWithFormat:@"%ld%ld",(long)indexPath.section,(long)indexPath.row];
[collectionView registerClass:[MoreGoodShopCollectionViewCell class] forCellWithReuseIdentifier:identifier];
MoreGoodShopCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
手势冲突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//注意传入的参数是当前的CollectionView对象
if ([touch.view isDescendantOfView:self.currentCollectionView]) {
return NO;
}
return YES;
}
多个UICollectionView
不能用同个UICollectionViewFlowLayout
目录
1、刷新无效
[collectionView reloadData];无效
以下两种方法均有效:(原因未知)
[self.collectionViewreloadItemsAtIndexPaths:@[[NSIndexPathindexPathForRow:7inSection:0]]];
或者
[self.collectionViewreloadSections:[NSIndexSetindexSetWithIndex:0]];
1.Cell的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGSize size = CGSizeMake(80,80);
return size;
}
2.每个Section的四边间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(15, 15, 5, 15);//分别为上、左、下、右
}
3.两行cell之间的间距(上下行cell的间距)
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
4.两个cell之间的间距(同一行的cell的间距)
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
5.拖拽排序
1)不带删除
@interface CollectionVVVVV :UIViewController
@end
#import "CollectionVVVVV.h"
@interface CollectionViewCelll : UICollectionViewCell
@property (nonatomic, strong) UILabel *title;
@property (nonatomic , copy) NSString *titleStr;
@end
@implementation CollectionViewCelll
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UILabel *title = [[UILabel alloc] init];
title.backgroundColor = [UIColor whiteColor];
title.frame = self.contentView.bounds;
[self.contentView addSubview:title];
self.title = title;
}
return self;
}
-(void)cellTitleStr:(NSString *)titleStr{
self.title.text = titleStr;
}
@end
@interface CollectionVVVVV ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic , strong) UICollectionView *collectionV;
@property (nonatomic , strong) NSMutableArray *dataArray;
@end
#define SCwidth self.view.frame.size.width
@implementation CollectionVVVVV
-(UICollectionView *)collectionV{
if (!_collectionV) {
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(SCwidth/4-15, SCwidth/4 -15);
flowLayout.minimumLineSpacing = 10;
flowLayout.minimumInteritemSpacing = 10;
flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
_collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.originaHeight, SCwidth, self.view.frame.size.height) collectionViewLayout:flowLayout];
_collectionV.delegate = self;
_collectionV.dataSource = self;
// _collectionV.backgroundColor = [UIColor grayColor];
[_collectionV registerClass:[CollectionViewCelll class] forCellWithReuseIdentifier:@"cell"];
_collectionV.backgroundColor = [UIColor orangeColor];
}
UILongPressGestureRecognizer *longp = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPress:)];
[_collectionV addGestureRecognizer:longp];
return _collectionV;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.dataArray = [NSMutableArray array];
for (int i = 0; i <20 ; i ++) {
NSString *title = [NSString stringWithFormat:@"南玻 %d",i];
[self.dataArray addObject:title];
}
[self.view addSubview:self.collectionV];
[self.collectionV reloadData];
self.view.backgroundColor = [UIColor whiteColor];
}
-(void)LongPress:(UILongPressGestureRecognizer*)longp{
CGPoint point = [longp locationInView:self.collectionV];
NSIndexPath *index = [self.collectionV indexPathForItemAtPoint:point];
CollectionViewCelll *cell = (CollectionViewCelll*)[self.collectionV cellForItemAtIndexPath:index];
switch (longp.state) {
case UIGestureRecognizerStateBegan:
{
[UIView animateWithDuration:0.2 animations:^{
cell.transform = CGAffineTransformMakeScale(1.3, 1.3);
} completion:^(BOOL finished) {
}];
if (!index) {
break;
}
BOOL canMove = [self.collectionV beginInteractiveMovementForItemAtIndexPath:index];
if (!canMove) {
break;
}
}
break;
case UIGestureRecognizerStateChanged:
{
[self.collectionV updateInteractiveMovementTargetPosition:point];
}
break;
case UIGestureRecognizerStateEnded:
{
[self.collectionV endInteractiveMovement];
}
break;
default:
{
[self.collectionV cancelInteractiveMovement];
}
break;
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.dataArray.count;
}
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSString *temp = [_dataArray objectAtIndex:sourceIndexPath.row];
[_dataArray removeObjectAtIndex:sourceIndexPath.row];
[_dataArray insertObject:temp atIndex:destinationIndexPath.row];
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCelll* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
[cell.title setText:_dataArray[indexPath.row]];
return cell;
}
@end
2)带删除
1.先懒加载数据以及CollectionView
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UICollectionView *collectionView;
- (NSMutableArray *)dataArr {
if (!_dataArr) {
_dataArr = [NSMutableArray new];
for (int i = 0 ; i < 20; i ++) {
[_dataArr addObject:[NSString stringWithFormat:@"%d",i]];
}
}
return _dataArr;
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor whiteColor];
[_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
}
return _collectionView;
}
2.实现collectionView的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.dataArr count];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(100, 50);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = self.dataArr[indexPath.item];
cell.deleteBtn.hidden = YES;
return cell;
}
3.在collectionView上添加一个长按手势
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(moveAction:)];
_collectionView.userInteractionEnabled = YES;
[_collectionView addGestureRecognizer:longPressGesture];
4.实现手势的方法
- (void)moveAction:(UILongPressGestureRecognizer *)longGes {
if (longGes.state == UIGestureRecognizerStateBegan) {
NSIndexPath *selectPath = [self.collectionView indexPathForItemAtPoint:[longGes locationInView:longGes.view]];
CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:selectPath];
cell.deleteBtn.hidden = NO;
[cell.deleteBtn addTarget:self action:@selector(deleteItemAction:) forControlEvents:UIControlEventTouchUpInside];
cell.deleteBtn.tag = selectPath.item;
[self.collectionView beginInteractiveMovementForItemAtIndexPath:selectPath];
}else if (longGes.state == UIGestureRecognizerStateChanged) {
[self.collectionView updateInteractiveMovementTargetPosition:[longGes locationInView:longGes.view]];
}else if (longGes.state == UIGestureRecognizerStateEnded) {
[self.collectionView endInteractiveMovement];
}else {
[self.collectionView cancelInteractiveMovement];
}
}
5.删除cell方法
- (void)deleteItemAction:(UIButton *)btn {
[self.dataArr removeObjectAtIndex:btn.tag];
[self.collectionView reloadData];
}
6.移动cell方法
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
id obj = self.dataArr[sourceIndexPath.item];
[self.dataArr removeObjectAtIndex:sourceIndexPath.item];
[self.dataArr insertObject:obj atIndex:destinationIndexPath.item];
[self.collectionView reloadData];
}
7.不显示滑动条
View.showsVerticalScrollIndicator = NO;
View.showsHorizontalScrollIndicator = NO;
8.设置可滑动
self.myCollectionView.alwaysBounceVertical = YES;
9.头尾视图
非自定义
[_collectView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
// 注册尾部
[_collectView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
// 如果当前想要的是头部视图
// UICollectionElementKindSectionHeader是一个const修饰的字符串常量,所以可以直接使用==比较
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
return headerView;
} else { // 返回每一组的尾部视图
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot" forIndexPath:indexPath];
return footerView;
}
}
// 设置区头尺寸高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
CGSize size = CGSizeMake(ScreenWidths, 60);
return size;
}
// 设置区尾尺寸高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
CGSize size = CGSizeMake(ScreenWidths, 30);
return size;
}
自定义
// 注册区头
[_collectionView registerClass:[CNCollectionReusableHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];
// 注册区尾
[_collectionView registerClass:[CNCollectionReusableFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier];
// 设置区头尺寸高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
CGSize size = CGSizeMake(SCREEN_WIDTH, 60);
return size;
}
// 设置区尾尺寸高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
CGSize size = CGSizeMake(SCREEN_WIDTH, 1);
return size;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
UICollectionReusableView *reusableView = nil;
// 区头
if (kind == UICollectionElementKindSectionHeader) {
CNCollectionReusableHeaderView *headerView = (CNCollectionReusableHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
headerView.titleLab.text = _titleArray[indexPath.section];
reusableView = headerView;
}
// 区尾
if (kind == UICollectionElementKindSectionFooter) {
CNCollectionReusableFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier forIndexPath:indexPath];
reusableView = footerView;
}
return reusableView;
}
10、居左显示