本帖最后由 竹林风 于 2019-1-7 17:25 编辑
导读
先看效果:
新建一个类 SelectCity,看代码:
SelectCity.h
[Objective-C] 纯文本查看 复制代码 #import "BaseViewController.h"
typedef void(^DidSelectCity)(NSString *cityName);
@interface SelectCityVC : BaseViewController
@property (nonatomic,assign) NSInteger selectCityType;
@property(nonatomic,copy) DidSelectCity didSelectCity;
@end
SelectCity.m
[Objective-C] 纯文本查看 复制代码 #import "SelectCityVC.h"
#import "TitleView.h"
#import "DSCollectionView.h"
#import "StickyHeaderLayout.h"
#import "TextCollectionCell.h"
static NSString *TextCollectionHeaderIdentifier = @"TextCollectionHeaderView";
static NSString *TextCollectionCellIdentifier = @"TextCollectionCell";
@interface SelectCityVC ()<UICollectionViewDataSource,UICollectionViewDelegate,DSCollectionViewDelegate>{
NSArray *aryProvince;//所有城市
NSMutableArray *mAryIndex;//字母索引
UICollectionViewFlowLayout *flowLayout;
DSCollectionView *_collectionView;
}
@end
@implementation SelectCityVC
-(void)initData{
NSString * path = [[NSBundle mainBundle]pathForResource:@"IndexCitys" ofType:@"plist"];
aryProvince = [NSArray arrayWithContentsOfFile:path];
if (!mAryIndex) {
mAryIndex = [@[] mutableCopy];
}
for (int i = 0; i < aryProvince.count; i ++) {
NSDictionary *dic = aryProvince[i];
[mAryIndex addObject:dic[@"TopIndex"]];
}
[_collectionView.collectionView reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initData];
flowLayout=[[StickyHeaderLayout alloc] init];
// flowLayout.itemSize=CGSizeMake(self.view.frame.size.width/3,self.view.frame.size.width/3);
flowLayout.sectionInset = UIEdgeInsetsMake(0,25,0,25);
flowLayout.minimumInteritemSpacing = 8;
flowLayout.minimumLineSpacing=8;
_collectionView = [[DSCollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,FrameHeight)];
_collectionView.delegate=self;
_collectionView.beginSectionIndex = 0;
[_collectionView.collectionView setCollectionViewLayout:flowLayout];
_collectionView.collectionView.alwaysBounceVertical=YES;
[_collectionView.collectionView setIndicatorStyle:UIScrollViewIndicatorStyleWhite];
[_collectionView.collectionView setBackgroundColor:[UIColor whiteColor]];
[_collectionView.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:TextCollectionHeaderIdentifier];
[_collectionView.collectionView registerClass:[TextCollectionCell class] forCellWithReuseIdentifier:TextCollectionCellIdentifier];
[self.view addSubview:_collectionView];
}
#pragma mark - UICollectionViewDelegate
#pragma mark - sectionHeader
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return aryProvince.count;
}
//设置collectionViewSection 的edgeInset
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
UIEdgeInsets edgeInset=UIEdgeInsetsMake(0,25,0,25);
return edgeInset;
}
// 设置section头视图的参考大小,与tableheaderview类似
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(ScreenWidth, 40);
}
//创建头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *vHeader = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:TextCollectionHeaderIdentifier forIndexPath:indexPath];
TitleView *vTitle = (TitleView *)[vHeader viewWithTag:1001];
if (!vTitle) {
vTitle = [[TitleView alloc]initWithFrameHeight:40 LeftMargin:15];
vTitle.titleLabel.textColor = Color_333;
vTitle.titleLabel.font = kFont(14);
vTitle.tag = 1001;
[vHeader addSubview:vTitle];
}
NSDictionary *dic = aryProvince[indexPath.section];
vTitle.titleLabel.text = dic[@"TopIndex"];
return vHeader;
}
-(NSArray *)sectionIndexTitlesForDSCollectionView:(DSCollectionView *)tableView{
return mAryIndex;
}
#pragma mark - collectionCell
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSDictionary *dic = aryProvince[section];
NSArray *aryCitys = dic[@"citys"];
return aryCitys.count;
}
//设置cell的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dic = aryProvince[indexPath.section];
NSArray *aryCitys = dic[@"citys"];
NSString *city = aryCitys[indexPath.row];
return [TextCollectionCell getCollectionCellSizeByText:city];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
return [self getTextCollectionCellForCollectionView:collectionView AtIndexPath:indexPath];
}
-(TextCollectionCell *)getTextCollectionCellForCollectionView:(UICollectionView *)collectionView AtIndexPath:(NSIndexPath *)indexPath{
TextCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TextCollectionCellIdentifier forIndexPath:indexPath];
NSDictionary *dic = aryProvince[indexPath.section];
NSArray *aryCitys = dic[@"citys"];
NSString *city = aryCitys[indexPath.row];
[cell updateCollectionCellByText:city];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *dic = aryProvince[indexPath.section];
NSArray *aryCitys = dic[@"citys"];
NSString *city = aryCitys[indexPath.row];
_didSelectCity([NSString stringWithFormat:@"%@",city]);
popViewController(self, YES);
}
注意:viewDidLoad 方法里面 数据初始化要先于DSCollectionView 的初始化 。不然索引是不会显示的。
附件:
带索引标签宽度自适应显示.zip
(757.71 KB, 下载次数: 1)
|