Skip to content

DJCollectionViewVM is a lightweight ViewModel implementation for UICollectionView.

License

Notifications You must be signed in to change notification settings

Dokay/DJCollectionViewVM

Repository files navigation

DJCollectionViewVM

License MIT Pod version Carthage Compatible Platform info

What

DJCollectionViewVM is a lightweight ViewModel implementation for UICollectionView.

Features

  • less code and more flexible to implement linear layout using UICollectionView with UICollectionViewFlowLayout;
  • dynamic cell and SupplementaryView size caculate;
  • header and footer support like UITableView;
  • prefetch for iOS 7.0+;

Requirements

  • Xcode 7 or higher
  • Apple LLVM compiler
  • iOS 7.0 or higher
  • ARC

Demo

Build and run the DJComponentCollectionViewVM.xcodeproj in Xcode.

Installation

CocoaPods

Edit your Podfile and add DJCollectionViewVM:

pod 'DJCollectionViewVM'

Quickstart

  • Sample code
- (void)testNormal
{
    DJCollectionViewVMCellRegister(self.collectionVM, DJCollectionViewTitleCellRow, DJCollectionViewTitleCell);
    [self.collectionVM removeAllSections];
    
    DJCollectionViewVMSection *contentSection = [DJCollectionViewVMSection sectionWithHeaderHeight:10];
    contentSection.minimumLineSpacing = 10.0f;
    contentSection.minimumInteritemSpacing = 10.0f;
    [self.collectionVM addSection:contentSection];
    for (NSInteger i = 0; i < 100; i ++) {
        DJCollectionViewTitleCellRow *row = [DJCollectionViewTitleCellRow new];
        row.itemSize = CGSizeMake(100, 100);
        row.backgroundColor = [UIColor redColor];
        row.title = [NSString stringWithFormat:@"%@",@(i)];
        [row setSelectionHandler:^(DJCollectionViewVMRow *rowVM) {
            NSLog(@"tap %@",rowVM.indexPath);
        }];
        [contentSection addRow:row];
    }
    
    [self.collectionView reloadData];
}
  • API
Key Classes
DJCollectionViewVM The ViewModel for UICollectionView, which has implemented UICollectionViewDelegate , UICollectionViewDataSource and UICollectionViewDelegateFlowLayout. It has multiple DJCollectionViewVMSection sections.
DJCollectionViewVMSection The ViewModel for sections in DJCollectionViewVM, each section has multiple DJCollectionViewVMRow rows.
DJCollectionViewVMRow The ViewModel for rows in section,it is the root class of all DJCollectionViewVM row hierarchies.
You should subclass DJCollectionViewVMRow to obtain cell characteristics specific to your application's needs. Through DJCollectionViewVMRow, rows inherit a basic interface that communicates with DJCollectionViewVM and DJCollectionViewVMSection.
DJCollectionViewVMCell The View for DJCollectionViewVMRow(ViewModel),it defines the attributes and behavior of the cells that appear in UICollectionView objects. You should subclass DJCollectionViewVMCell to obtain cell characteristics and behavior specific to your application's needs. By default, it is being mapped with DJCollectionViewVMRow.
DJCollectionViewVMReusable The ViewModel for supplementary view in section header and footer,it is the root class of all DJCollectionViewVM supplementary view hierarchies.
You should subclass DJCollectionViewVMReusable to obtain supplementary view characteristics specific to your application's needs. Through DJCollectionViewVMReusable, supplementary views inherit a basic interface that communicates with DJCollectionViewVM and DJCollectionViewVMSection.
DJCollectionViewVMReusableView The View for DJCollectionViewVMReusable(ViewModel),it defines the attributes and behavior of the supplementary views that appear in UICollectionView objects. You should subclass DJCollectionViewVMReusableView to obtain supplementary view characteristics and behavior specific to your application's needs. By default, it is being mapped with DJCollectionViewVMReusable.
  • For exsist cells in your project
    1.If the super class of your cell is UICollectionViewCell ,just change it to DJCollectionViewVMCell.
    2.If tht super class of your cell is your custom class ,you need to implement the protocol DJCollectionViewVMCellDelegate or change it to DJCollectionViewVMCell.

DJTableViewVM

ViewModel for UITableView: DJTableViewVM

Contact

Dokay Dou

License

DJCollectionViewVM is available under the MIT license.

About

DJCollectionViewVM is a lightweight ViewModel implementation for UICollectionView.

Resources

License

Stars

Watchers

Forks

Packages

No packages published