UICollectionView 索引,城市列表

#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;
}

 

转载于:https://my.oschina.net/iOScoderZhao/blog/783853

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值