美文网首页实用轮子
UICollectionView的基本用法及Collection

UICollectionView的基本用法及Collection

作者: WSGNSLog | 来源:发表于2016-10-26 18:13 被阅读221次

自定义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
/** 分区头部名称 */
@property (nonatomic,strong) NSString * secTimeTitle;
/** 每个分组的数组 */
@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;//设置允许多选
[self.view addSubview:_collectionView];
}

pragma mark - UICollectionViewDataSource

// Section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return self.dataSource.count;
}

// section中Cell的个数
- (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);
}

// 设置最小行间距,也就是前一行与后一行的中间最小间隔
- (CGFloat)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 10;
}

// 设置最小列间距
- (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;
}

// 高亮完成后回调
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {

}  

// 由高亮转成非高亮
- (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;//放在添加到数组后面,否则第一个索引会为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)];

}

相关文章

网友评论

  • narutog17:能发个demo么?
    narutog17:@WSGNSLog 感谢
    WSGNSLog:@narutog17 https://github.com/WSGNSLog/CollectionViewMultySelected

本文标题:UICollectionView的基本用法及Collection

本文链接:https://www.haomeiwen.com/subject/fyxjuttx.html