本帖最后由 竹林风 于 2019-1-7 17:25 编辑
导读
效果:
1.首先新建一个页面SelectCityVC继承于 BaseCollectionViewController
直接上代码:
SelectCityVC.h
[Objective-C] 纯文本查看 复制代码 #import "BaseCollectionViewController.h"
typedef void(^DidSelectCity)(NSString *cityName);
@interface SelectCityVC : BaseCollectionViewController
@property (nonatomic,assign) NSInteger selectCityType;
@property(nonatomic,copy) DidSelectCity didSelectCity;
@end
SelectCityVC.m
[Objective-C] 纯文本查看 复制代码 #import "SelectCityVC.h"
#import "TitleView.h"
#import "JobFuncLayout.h"
#import "TextCollectionCell.h"
static NSString *TextCollectionHeaderIdentifier = @"TextCollectionHeaderView";
static NSString *TextCollectionCellIdentifier = @"TextCollectionCell";
@interface SelectCityVC ()<UICollectionViewDataSource,UICollectionViewDelegate>{
NSArray *aryProvince;//所有城市
UICollectionViewFlowLayout *flowLayout;
}
@end
@implementation SelectCityVC
-(void)initData{
NSString * path = [[NSBundle mainBundle]pathForResource:@"citys" ofType:@"plist"];
aryProvince = [NSArray arrayWithContentsOfFile:path];
[self.theCollectionView reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
JobFuncLayout * flowLayout = [[JobFuncLayout alloc]init];
flowLayout.minimumLineSpacing = 6;
flowLayout.minimumInteritemSpacing = 6;
flowLayout.sectionInset = UIEdgeInsetsMake(0, 25, 0, 25);
self.theCollectionView.collectionViewLayout = flowLayout;
self.theCollectionView.backgroundColor = kColor(whiteColor);
[self.theCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:TextCollectionHeaderIdentifier];
[self.theCollectionView registerClass:[TextCollectionCell class] forCellWithReuseIdentifier:TextCollectionCellIdentifier];
[self initData];
}
#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[@"name"];
return vHeader;
}
#pragma mark - collectionCell
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSDictionary *dic = aryProvince[section];
NSArray *aryCitys = dic[@"cities"];
return aryCitys.count;
}
//设置cell的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dic = aryProvince[indexPath.section];
NSArray *aryCitys = dic[@"cities"];
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[@"cities"];
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[@"cities"];
NSString *city = aryCitys[indexPath.row];
_didSelectCity([NSString stringWithFormat:@"%@%@",dic[@"name"],city]);
popViewController(self, YES);
}
附件:
标签宽度自适应显示.zip
(734.38 KB, 下载次数: 1)
|