自定義cell的model
#import <Foundation/Foundation.h>
@interface MCellModel : NSObject
/** 被選中的次序 */
@property (nonatomic,assign) int selectedIndex;
/** 被選中的次序 */
@property (nonatomic,strong) NSIndexPath* indexPath;
@property (nonatomic,assign) BOOL isSelect;
/** 被選中的索引 */
@property (nonatomic,strong) NSString *selectIndex;
@end
自定義section的model
#import <Foundation/Foundation.h>
@interface SectionModel : NSObject
/** 分區(qū)頭部名稱 */
@property (nonatomic,strong) NSString * secTimeTitle;
/** 每個分組的數(shù)組 */
@property (nonatomic,strong) NSArray * itemsArr;
@end
自定義cell
#import <UIKit/UIKit.h>
#import "MCellModel.h"
@interface MMediaCollectionCell : UICollectionViewCell
/** cellModel */
@property (nonatomic,strong) MCellModel * mCellModel;
/** 選中時的背景 */
@property (nonatomic,strong) UIImageView * selectBGImageV;
/** 選中時index */
@property (nonatomic,strong) UILabel * selecteIndexLabel;
@end
#import "MMediaCollectionCell.h"
@implementation MMediaCollectionCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backGroundColor = [UIColor orangeColor];
_selectBGImageV = [[UIImageView alloc]init];
_selecteIndexLabel = [[UILabel alloc]init];
[_selectBGImageV addSubview:_selecteIndexLabel];
[self addSubview:self.selectBGImageV];
_selectBGImageV.hidden = YES;
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat width = self.bounds.size.width;
CGFloat height = self.bounds.size.height;
_selectBGImageV.frame = self.bounds;
_selectBGImageV.layer.borderWidth = 5;
_selectBGImageV.layer.borderColor = [UIColor cyanColor].CGColor;
_selectBGImageV.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
_selecteIndexLabel.frame = CGRectMake(width/4, width/4, width/2, width/2);
_selecteIndexLabel.textAlignment = NSTextAlignmentCenter;
_selecteIndexLabel.font = [UIFont systemFontOfSize:25];
_selecteIndexLabel.textColor = [UIColor cyanColor];
}
- (void)setMCellModel:(MCellModel *)mCellModel
{
if (mCellModel.isSelect) {
self.selecteIndexLabel.text = [NSString stringWithFormat:@"%d",mCellModel.selectedIndex];
self.selectBGImageV.hidden = NO;
}else{
self.selectBGImageV.hidden = YES;
}
}
控制器
- (void)viewDidLoad {
[super viewDidLoad]
self.view.backgroundColor = [UIColor whiteColor];
[self setUpCollectionView];
}
- (void)setUpCollectionView{
UICollectionViewFlowLayout *flayout = [[UICollectionViewFlowLayout alloc]init];
flayout.itemSize = CGSizeMake(WWidth / 5, WWidth / 5);
flayout.minimumInteritemSpacing = 10;
flayout.minimumLineSpacing = 10;
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, WWidth, WHeight) collectionViewLayout:flayout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource =self;
_collectionView.delegate = self;
_collectionView.showsVerticalScrollIndicator = NO;
//注冊cell及頭腳視圖
[_collectionView registerClass:[MMediaCollectionCell class] forCellWithReuseIdentifier:ID];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderID];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FooterID];
_collectionView.autoresizingMask = NO;
_collectionView.allowsMultipleSelection = YES;//設(shè)置允許多選
[self.view addSubview:_collectionView];
}
pragma mark - UICollectionViewDataSource
// Section個數(shù)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return self.dataSource.count;
}
// section中Cell的個數(shù)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
SectionModel *secModel = self.dataSource[section];
return [[self.dataSource[section] itemsArr] count];
}
// cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MMediaCollectionCell *mCell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
SectionModel *secModel = _dataSource[indexPath.section];
MCellModel *model = secModel.itemsArr[indexPath.row];
cell.textLabel.text = model.showMessage;
return mCell;
}
pragma mark - 頭腳視圖
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HeaderID forIndexPath:indexPath];
SectionModel *secModel = self.dataSource[indexPath.section];
//解決重用問題
if (header.subviews.lastObject!=nil) {
[header.subviews.lastObject removeFromSuperview];
}
UILabel *sectionTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 200, 30)];
sectionTitleLabel.text = @"hahaha";
sectionTitleLabel.font = [UIFont systemFontOfSize:17];
[header addSubview:sectionTitleLabel];
return header;
}else if(kind == UICollectionElementKindSectionFooter){
UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:FooterID forIndexPath:indexPath];
return footer;
}else{
return nil;
}
}
pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(self.view.frame.size.width / 3 - 10, self.view.frame.size.width / 3 - 10);
}
// 每個cell上下左右相距
- (UIEdgeInsets)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(5, 5, 5, 5);
}
// 設(shè)置最小行間距细卧,也就是前一行與后一行的中間最小間隔
- (CGFloat)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 10;
}
// 設(shè)置最小列間距
- (CGFloat)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 10;
}
// section頭視圖的大小
- (CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(self.view.frame.size.width, 40);
}
// section尾視圖的大小
- (CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
return CGSizeMake(self.view.frame.size.width, 40);
}
pragma mark - UICollectionViewDelegate
// 允許選中時筒占,高亮
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s", FUNCTION);
return YES;
}
// 高亮完成后回調(diào)
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
}
// 由高亮轉(zhuǎn)成非高亮
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
}
// 是否允許選中
- (BOOL)collectionView:(UICollectionView *)collectionViewshouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
// 是否允許取消選中
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
// 選中操作
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
MMediaCollectionCell *cell = (MMediaCollectionCell *)[_collectionView cellForItemAtIndexPath:indexPath];
SectionModel *secModel = self.dataSource[indexPath.section];
MCellModel *itemModel = secModel.itemsArr[indexPath.row];
itemModel.isSelect = cell.isSelected;
itemModel.indexPath = indexPath;
itemModel.selectedIndex = self.selectedItemsArr.count +1;//放在添加到數(shù)組后面翰苫,否則第一個索引會為0
[self.selectedItemsArr addObject:itemModel];
cell.mCellModel = itemModel;
}
// 取消選中操作
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
MMediaCollectionCell *cell = (MMediaCollectionCell *)[_collectionView cellForItemAtIndexPath:indexPath];
SectionModel *secModel = self.dataSource[indexPath.section];
MCellModel *deselectModel = secModel.itemsArr[indexPath.row];
deselectModel.isSelect = cell.isSelected;
//取消選中的item索引
int deselectedIndex = deselectModel.selectedIndex;
cell.mCellModel = deselectModel;
//取消選中的不是最后一個時,把取消選中的這個后面的所有item重新排序
if (deselectedIndex < self.selectedItemsArr.count) {
for (int i = deselectedIndex; i<=self.selectedItemsArr.count; i++) {
MCellModel *mdel = self.selectedItemsArr[i-1];
mdel.selectedIndex-=1;
NSIndexPath *indexP = mdel.indexPath;
MMediaCollectionCell *refreshItem = (MMediaCollectionCell *)[_collectionView cellForItemAtIndexPath:indexP];
refreshItem.mCellModel = mdel;
}
}
[self.selectedItemsArr removeObjectAtIndex:(deselectedIndex-1)];
}