http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 3302|回复: 5

[综合] 【iOS开发实用框架】自适应宽度标签显示

[复制链接]
发表于 2018-12-25 17:15:33 | 显示全部楼层 |阅读模式
本帖最后由 竹林风 于 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

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)


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

本版积分规则

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

GMT+8, 2024-4-25 11:54

© 2014-2021

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