UICollectionView 使用總結

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;

 

 



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章