日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/18 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS基础之使用UICollectionView纯代码创建 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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纯代码创建的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。