Skip to content

An extension to UICollectionView that makes creating collectionviews efficiently and gracefully.

License

Notifications You must be signed in to change notification settings

Jun2786184671/JUNCollectionView

Repository files navigation

JUNCollectionView

Version License Platform

Demo

To run the demo project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

JUNCollectionView is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'JUNCollectionView'

Guide

  1. #import <JUNCollectionView/UICollectionView+JUNex.h> into your project's pch file, otherwise you will need to import this header in anywhere you want to quickly create a collection view.

  2. There are a couple of methods that you can use to create a collection view quickly and elegantly.

+ (instancetype)jun_collectionViewWithItems:(NSArray<UIView *> *)items direction:(UICollectionViewScrollDirection)direction;
+ (instancetype)jun_collectionViewWithItemsBuilder:(JUNCollectionViewItemsBuilder)itemsBuilder direction:(UICollectionViewScrollDirection)direction;
+ (instancetype)jun_collectionViewWithItemCount:(NSUInteger)itemCount itemBuilder:(JUNCollectionViewItemIndexBuilder)itemBuilder direction:(UICollectionViewScrollDirection)direction;
+ (instancetype)jun_collectionViewWithItemCountBuilder:(JUNCollectionViewCountBuilder)countBuilder itemBuilder:(JUNCollectionViewItemIndexBuilder)itemBuilder direction:(UICollectionViewScrollDirection)direction;
+ (instancetype)jun_collectionViewWithForEach:(NSArray<id> *)elements itemBuilder:(JUNCollectionViewItemForEachBuilder)itemBuilder direction:(UICollectionViewScrollDirection)direction;

Don't worry about not specifying some of the parameter types in these methods, when you type these methods in XCode, you'll immediately know what to do next.

  1. Here are some examples.
[UICollectionView jun_collectionViewWithItems:@[
    aLabel,
    aButton,
    aSwitch,
] direction:UICollectionViewScrollDirectionHorizontal];
[UICollectionView jun_collectionViewWithItemCountBuilder:^NSUInteger{
    return arc4random_uniform(20);
} itemBuilder:^UIView * _Nonnull(NSUInteger index) {
    switch (index % 3) {
        case 0:
            return aLabel;
        case 1:
            return aButton;
        default:
            return aSwitch;
    }
} direction:UICollectionViewScrollDirectionHorizontal];

[NSTimer scheduledTimerWithTimeInterval:1.0 repeats:true block:^(NSTimer * _Nonnull timer) {
    [collectionView reloadData];
}];
[UICollectionView jun_collectionViewWithForEach:elements itemBuilder:^UIView *(NSUInteger index, id  element) {
    if ([element isEqual:@"text"]) {
        return aUILabel;
    } else if ([element isEqual:@"button"]) {
        return aUIButton;
    } else {
        // Fall on other conditions...
    }
} direction:UICollectionViewScrollDirectionHorizontal];
  1. A created collection view can set the delegate and datasource as normal.
collectionView.delegate = anyDelegate;
collectionView.dataSouce = anyDataSource;
  1. Collection view created by the above methods has some additional properties.
@property(nonatomic, assign) CGFloat jun_minimumLineSpacing;
@property(nonatomic, assign) CGFloat jun_minimumInteritemSpacing;
@property(nonatomic, assign) CGFloat jun_itemSize;
@property(nonatomic, assign) UIEdgeInsets jun_inset;

You can go to <JUNCollectionView/UICollectionView+JUNex.h> to see in detail.

Author

Jun Ma, [email protected]

License

JUNCollectionView is available under the MIT license. See the LICENSE file for more info.

About

An extension to UICollectionView that makes creating collectionviews efficiently and gracefully.

Resources

License

Stars

Watchers

Forks

Packages

No packages published