生活随笔
收集整理的這篇文章主要介紹了
IOS基础之使用UICollectionView纯代码创建
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS基礎之使用UICollectionView純代碼創建
資料來自2016-5-12某站。
有一定的參考意義,
涉及plist 轉字典模型,UICollectionView使用純代碼加載到View里面。實現效果圖:
時間很趕,plist的數據隨便寫的,主要實現功能,和圖片自己網上截圖的。
源碼在我的主頁下。
后期我全部上傳到github。
#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
;
}
- (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
#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
#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
#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
#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
#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
#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
];
_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
#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
#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纯代码创建的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。