#import "ViewController.h"
// 自定义头部
#import "HeaderCollectionReusableView.h"
// 自定义cell
#import "MainCollectionViewCell.h"
// 需要导入的文件
#import "DSCollectionView.h"
#import "StickyHeaderLayout.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,DSCollectionViewDelegate>
@property (nonatomic,strong)DSCollectionView *maincollection;
@property (nonatomic,strong)NSMutableArray *array;
@property (nonatomic,strong)NSArray *cityList;
@property (nonatomic,strong)NSArray *dataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_dataArray = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"G",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"X",@"X"];
NSString *path = [[NSBundle mainBundle]pathForResource:@"cities" ofType:@"plist"];
_cityList = [NSArray arrayWithContentsOfFile:path];
StickyHeaderLayout *layout = [[StickyHeaderLayout alloc]init];
layout.itemSize = CGSizeMake(80, 40);
layout.headerReferenceSize = CGSizeMake(0, 30);
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
_maincollection = [[DSCollectionView alloc]initWithFrame:self.view.bounds];
_maincollection.backgroundColor = [UIColor whiteColor];
_maincollection.delegate = self;
[_maincollection.collectionView setCollectionViewLayout:layout];
_maincollection.collectionView.alwaysBounceVertical=YES;
[_maincollection.collectionView setIndicatorStyle:UIScrollViewIndicatorStyleWhite];
_maincollection.collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_maincollection];
[_maincollection.collectionView registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier:@"MainCollectionViewCell"];
// 头部注册
[_maincollection.collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"heaer"];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return _cityList.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSArray *city = _cityList[section];
return city.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MainCollectionViewCell" forIndexPath:indexPath];
NSArray *city = _cityList[indexPath.section];
cell.mainLabel.text = city[indexPath.row];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
// 自定义header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
HeaderCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"heaer" forIndexPath:indexPath];
view.backgroundColor = [UIColor grayColor];
view.nameLabel.text = _dataArray[indexPath.section];
return view;
}
// 自适应cell
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *array = _cityList[indexPath.section];
NSString *string = array[indexPath.row];
CGFloat width = [MainCollectionViewCell getCellWidthWith:string];
NSLog(@"%f",width);
return CGSizeMake(width + 20, 40);
}
// 边框
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(10, 10, 10, 20);
}
// 最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 20;
}
// 最小列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 15;
}
// 索引数组
-(NSArray *)sectionIndexTitlesForDSCollectionView:(DSCollectionView *)tableView{
return _dataArray;
}