苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 5132|回复: 4

[综合] 【iOS开发实用框架】带索引标签列表宽度自适应

[复制链接]
发表于 2019-1-4 16:14:19 | 显示全部楼层 |阅读模式
本帖最后由 竹林风 于 2019-1-7 17:25 编辑



   导读

【iOS开发实用框架】基础教程目录    http://www.sufeinet.com/thread-24348-1-1.html
iOS开发实用框架】实例目录贴    http://www.sufeinet.com/thread-24654-1-1.html



先看效果:

4.gif

新建一个类 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)


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2019-1-4 16:24:58 | 显示全部楼层
强烈支持楼主ing……
发表于 2019-1-5 10:13:36 | 显示全部楼层
我只是路过打酱油的。
发表于 2019-1-5 13:56:24 | 显示全部楼层
强烈支持楼主ing……
发表于 2019-1-5 21:03:33 | 显示全部楼层
我只是路过打酱油的。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-12-27 00:03

© 2014-2021

快速回复 返回顶部 返回列表