IOS基础之使用UICollectionView纯代码创建

资料来自2016-5-12某站。
有一定的参考意义,
涉及plist 转字典模型,UICollectionView使用纯代码加载到View里面。实现效果图:
时间很赶,plist的数据随便写的,主要实现功能,和图片自己网上截图的。

源码在我的主页下。
后期我全部上传到github。

//
//  ViewController.m
//  collection-后盾网
//
//  Created by 鲁军 on 2021/4/11.
//#import "ViewController.h"
#import "Group.h"
#import "ItemCell.h"
#import "HeaderView.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong) NSArray *dataArray;
@end
@implementation ViewController
- (NSArray *)dataArray{if(_dataArray == nil){_dataArray = [NSArray array];NSString *path = [[NSBundle mainBundle] pathForResource:@"data.plist" ofType:nil];NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];NSMutableArray *mArray = [NSMutableArray array];NSArray *nameArray = @[@"宅男",@"妇女",@"屌丝"];int i=0;for(NSString *keyName in dict){NSArray *array = dict[keyName];Group *group =[Group groupWithArray:array];group.groupName = nameArray[i++];[mArray addObject:group];}_dataArray = mArray.copy;}return _dataArray;
}
- (void)viewDidLoad {[super viewDidLoad];NSLog(@"%@",NSStringFromCGRect(self.view.frame));UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc] init];layOut.scrollDirection = UICollectionViewScrollDirectionVertical;UICollectionView *collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layOut];collection.backgroundColor = [UIColor whiteColor];collection.dataSource = self;collection.delegate = self;[self.view addSubview:collection];[collection registerClass:[ItemCell class] forCellWithReuseIdentifier:@"cell"];[collection registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}
#pragma mark 数据源方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{return  self.dataArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{static NSString *ID = @"cell";ItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];Group *group = self.dataArray[indexPath.section];Item *it = group.itemArray[indexPath.row];cell.item = it;return cell;
}
//告知每组有多少个item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{Group  *group = self.dataArray[section];return group.itemArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *) collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{return CGSizeMake((320-40)/3, 140);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *) collectionViewLayout insetForSectionAtIndex:(NSInteger)section{return UIEdgeInsetsMake(20, 10, 10, 10);}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{HeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];Group *group =self.dataArray[indexPath.section];headerView.groupName = group.groupName;return headerView;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{return CGSizeMake(320, 60);
}
@end
//
//  Item.h
//  collection-后盾网
//
//  Created by 鲁军 on 2021/4/11.
//#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN@interface Item : NSObject
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *imgsrc;
-(instancetype)initWithDict:(NSDictionary *)dict;
+(id)itemWithDict:(NSDictionary *)dict;
@endNS_ASSUME_NONNULL_END
//
//  Item.m
//  collection-后盾网
//
//  Created by 鲁军 on 2021/4/11.
//#import "Item.h"
@implementation Item
- (instancetype)initWithDict:(NSDictionary *)dict{if(self = [super init]){self.title = dict[@"title"];self.imgsrc = dict[@"image_data"];}return  self;
}+ (id)itemWithDict:(NSDictionary *)dict{return  [[self alloc] initWithDict:dict];
}
@end
//
//  Group.h
//  collection-后盾网
//
//  Created by 鲁军 on 2021/4/11.
//#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN@interface Group : NSObject
@property(nonatomic,copy)NSString *groupName;
@property(nonatomic,strong)NSMutableArray *itemArray;
-(instancetype)initWithArray:(NSArray *) array;
+(id)groupWithArray:(NSArray *)array;
@end
NS_ASSUME_NONNULL_END
//
//  Group.m
//  collection-后盾网
//
//  Created by 鲁军 on 2021/4/11.
//#import "Group.h"
#import "Item.h"
@implementation Group-(instancetype)initWithArray:(NSArray *) array{if(self = [super init]){_itemArray = [NSMutableArray array];for(NSDictionary *dict in array){Item *item = [Item itemWithDict:dict];[_itemArray addObject:item];}}return self;
}
+(id)groupWithArray:(NSArray *)array{return [[self alloc] initWithArray:array];
}@end
//
//  ItemCell.h
//  collection-后盾网
//
//  Created by 鲁军 on 2021/4/11.
//#import <UIKit/UIKit.h>
#import "Item.h"
NS_ASSUME_NONNULL_BEGIN@interface ItemCell : UICollectionViewCell@property(nonatomic,strong)Item *item;@property(nonatomic,weak)UIImageView *iconView;@property(nonatomic,weak)UILabel *textView;@endNS_ASSUME_NONNULL_END
//
//  ItemCell.m
//  collection-后盾网
//
//  Created by 鲁军 on 2021/4/11.
//#import "ItemCell.h"@implementation ItemCell
- (instancetype)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {UIImageView *iconView = [[UIImageView alloc]init];_iconView =iconView;[self.contentView addSubview:iconView];UILabel *textView = [UILabel new];_textView = textView;[self.contentView addSubview:textView];}return self;
}- (void)setItem:(Item *)item{_item = item;[self settingData];[self settingFrame];
}
-(void)settingData{_iconView.image = [UIImage imageNamed:self.item.imgsrc];
//    [self.iconView sizeToFit];_textView.text = self.item.title;}-(void)settingFrame{_iconView.frame=CGRectMake(0, 0, (320-40)/3, 120);_textView.frame = CGRectMake(0, 120, (320-40)/3, 20);}@end
//
//  HeaderView.h
//  collection-后盾网
//
//  Created by 鲁军 on 2021/5/9.
//#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface HeaderView : UICollectionReusableView
@property(nonatomic,strong)NSString *groupName;
@property(nonatomic,weak)UILabel *textView;@endNS_ASSUME_NONNULL_END
//
//  HeaderView.m
//  collection-后盾网
//
//  Created by 鲁军 on 2021/5/9.
//#import "HeaderView.h"@implementation HeaderView- (instancetype)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {UILabel *textLabel = [UILabel new];_textView = textLabel;[self addSubview:textLabel];}return self;
}- (void)setGroupName:(NSString *)groupName{_groupName = groupName;[self settingData];[self settingFrame];
}
-(void)settingData{_textView.text = _groupName;
}-(void)settingFrame{_textView.frame = CGRectMake(0, 0, 320, 100);
}
@end

IOS基础之使用UICollectionView纯代码创建相关推荐

  1. iOS回顾笔记( 01 )-- XIB和纯代码创建应用的对比

    iOS回顾笔记( 01 )--  XIB和纯代码创建应用的对比 很多时候我们工作很久突然闲下来的时候,是不是也感到无聊过?这就是我现在的生活,不过闲一段时间也挺好,可以好好回顾一下自己以前学习iOS路 ...

  2. Swift5.x使用纯代码创建NavigationTab控制器设置启动图Wb第1部分

    Swift5.x使用纯代码创建NavigationTab控制器设置启动图Wb第1部分 前言 1 使用纯代码创建NavigationTab控制器必须会,也可使用storyboard拖控件,没有代码创建灵 ...

  3. ios14.3开发之使用纯代码创建UITabbarController

    ios14.3开发之使用纯代码创建UITabbarController 这里我们IOS版本14.3.Xcode 版本选择的是.12.3版本:来自apple最新的xcode版本,该版本支持最新的swif ...

  4. 【Swift Mac开发】纯代码创建NSViewController

    对于习惯了纯代码iOS开发的人来说,刚接触Mac端开发时,是非常不友好的,网上所有的教程都是通过Xib或StoryBoard的方式创建NSViewController,而且苹果文档也是鼓励用Xib的开 ...

  5. IOS14.3开发之使用纯代码创建UIButton以及弹框的使用

    IOS14.3开发之使用纯代码创建UIButton以及弹框的使用 源码在这 // // ViewController.m // testDemo1 // // Created by 鲁军 on 202 ...

  6. IOS基础学习日志(七)利用dispatch_once创建单例及使用

    自苹果引入了Grand Central Dispatch (GCD)(Mac OS 10.6和iOS4.0)后,创建单例又有了新的方法,那就是使用dispatch_once函数,当然,随着演进的进行. ...

  7. iOS 使用纯代码或xib创建圆角视图

    尊重原创 转自:http://www.jianshu.com/p/80f1fd3f63a0 引言: 在我们日常开发中, 很多中情况下我们需要设置UIView或者UIImageView的圆角以及边框等, ...

  8. storyboard搭建项目_简单谈谈ios程序界面实现的三种方式(代码创建,xib和storyboard)...

    一丶前言 实现ios界面总的来说,有三种方式,传统的是纯代码创建与xib创建,近年来,苹果官网一直推荐用storyboard管理项目界面,最新的xcode 创建的project也是默认为storybo ...

  9. IOS15最标准的纯代码搭建项目

    IOS15最标准的纯代码搭建项目 环境 xCode 13 ios 15 如今手机产品更新日新月异.ios既要适配iPhone12,13这样的刘海机型,还要适配iphone8-5s这样的老机型.如何使用 ...

最新文章

  1. 人工智能正在引领全球企业的创新
  2. mysql 多个字段拼接
  3. 用tolower()和toupper()来实现对string进行大小写转换
  4. Linux下VSFTP配置全攻略
  5. C++动态数组简单的模拟二元堆
  6. 考研失败了,该何去何从?
  7. java例子:数组 数3退1
  8. Anaconda3+PyTorch安装教程
  9. Gin 快速入门知识点总结(奇淼)
  10. MySQL数据库规范及解读
  11. 跨线程操作无效:从创建该线程的线程以外的线程访问控件
  12. android15版本号手机,Lineage OS 15.1正式发布:基于Android 8.1
  13. 游戏开发之友元类和友元函数(C++基础)
  14. vb2010 连接mysql,VB连接数据库方式汇总
  15. 服务器系统安装蓝牙驱动,win10蓝牙驱动怎么安装?-win10蓝牙驱动的安装教程 - 河东软件园...
  16. 教你怎么利用Matlab画散点图
  17. JAVA案例之使用接口实现手机功能
  18. android外接键盘打汉字,外接键盘情况下,安卓打字不如windows
  19. 1g等于多少mb计算机网络,1g等于多少mb 硬盘容量详细介绍
  20. lisp 车位块自动编号_CAD exPRess tools 车位自动编号

热门文章

  1. 周期三角波傅里叶级数例题_傅里叶详解之傅里叶级数
  2. nginx php实例,多个mysql,nginx,php实例环境安装zabbix(完全自定义)
  3. lambda表达式python啥意思_Python中lambda表达式是什么
  4. oracle数据库的select,Oracle数据库--基本的select语句
  5. verilog中function用法_verilog中的function用法与例子
  6. python学习之turtle库基本操作
  7. 运动会成绩管理系统python_Python 实现简易版成绩管理系统
  8. logging 日志输出乱码 info_接口自动化测试中logging实际用法
  9. 未来计算机打蜡机,电脑洗车机的未来发展方向:多样化or精简化?
  10. win7可以安装sqlserver2008 企业版吗_MYSQLServer2008R2详细的图文安装教程