Giter Site home page Giter Site logo

irons163 / ircollectiontableviewmodel Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 799 KB

IRCollectionTableViewModel is a powerful MVVM Tableview/CollectionView for iOS, which is flexible and can easy to handle and reuse.

License: MIT License

Objective-C 86.75% Ruby 13.25%
mvvm mvvm-architecture mvvm-pattern tableview collectionview tableview-collectionview ios objcective-c flexible

ircollectiontableviewmodel's Introduction

Build Status Platform

IRCollectionTableViewModel

  • IRCollectionTableViewModel is a powerful MVVM Tableview/CollectionView for iOS, which is flexible and can easy to handle and reuse.

Features

  • MVVM structure.
  • Flexible, Reusable.

Install

Git

  • Git clone this project.
  • Copy this project into your own project.
  • Add the .xcodeproj into you project and link it as embed framework.

Options

  • You can remove the demo and ScreenShots folder.

Cocoapods

  • Add pod 'IRCollectionTableViewModel' in the Podfile
  • pod install

Introduction MVVM

  • Model--view--viewmodel (MVVM) is a software architectural pattern.
  • It has advantages more than the tranditional MVC architectural. Can improve the whole code strurcture.
  • More detail can see the MVVM wikipedia. MVVM

Usage

Basic

TableView

  • Create a new class TableViewViewModel extends TableViewBasicViewModel<UITableViewDataSource>, and Import IRCollectionTableViewModel
#import <IRCollectionTableViewModel/IRCollectionTableViewModel.h>

@interface TableViewViewModel : TableViewBasicViewModel<UITableViewDataSource>

@end
  • You can add your init method and register the cell inside
- (instancetype)initWithTableView:(UITableView*)tableView;

...

- (instancetype)initWithTableView:(UITableView *)tableView {
    if (self = [super init]) {
        items = [[NSMutableArray<id<SectionModelItem>> alloc] init];
        
        [tableView registerNib:[UINib nibWithNibName:CELL_NIB_NAME bundle:nil] forCellReuseIdentifier:CELL_IDENTIFIER];
    }
    return self;
}
  • Add update method
- (void)update;

...

- (void)update {
    [items removeAllObjects];
    // Setup items
    // [self setupRows];
}
  • For setup items, other words, setup the sections/rows you want to show. Create TableViewSectionItem and TableViewRowItem, DemoSectionType, DemoRowType
typedef NS_ENUM(NSInteger, DemoSectionType){
    DemoSection
};

typedef NS_ENUM(NSInteger, DemoRowType){
    RowType_DemoRow
};

@interface TableViewRowItem : RowBasicModelItem
@property (readonly) DemoRowType type;
@end

@interface TableViewSectionItem : SectionBasicModelItem
@property (nonatomic) NSString* sectionTitle;
@property (nonatomic) SectionType type;
@end
  • Implementation TableViewSectionItem and TableViewRowItem in the TableViewViewModel.m
@implementation TableViewRowItem
@dynamic type;
@end

@implementation TableViewSectionItem
@end
  • Setup items
- (void)setupRows {
    NSMutableArray *rowItems = [NSMutableArray array];
    [rowItems addObject:[[TableViewRowItem alloc] initWithType:RowType_DemoRow withTitle:@"Demo Row"]];
    [rowItems addObject:[[TableViewRowItem alloc] initWithType:RowType_DemoRow withTitle:@"Demo Row"]];
    
    NSArray *demoRowItems = [NSArray arrayWithArray:rowItems];
    TableViewSectionItem *item = [[TableViewSectionItem alloc] initWithRowCount:[demoRowItems count]];
    item.type = DemoSection;
    item.sectionTitle = @"Demo Section";
    item.rows = demoRowItems;
    [items addObject:item];
}
  • Override UITableViewDataSource
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return items.count;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [items[section] rowCount];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    id<SectionModelItem> item = [items objectAtIndex:indexPath.section];
    TableViewRowItem *row = (TableViewRowItem *)[item.rows objectAtIndex:[indexPath row]];
    
    switch (item.type) {
        case DemoSection:
        {
            switch (row.type) {
                case RowType_DemoRow:
                {
                    TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:TableViewCell.identifier forIndexPath:indexPath];
                    cell.titleLabel.text = [NSString stringWithFormat:@"%@%ld", row.title, row.tagRange.location];
                    cell.editTextField.text = [editedTexts objectAtIndex:indexPath.row];
                    cell.editTextField.tag = row.tagRange.location;
                    cell.editTextField.delegate = self;
                    return cell;
                }
            }
            break;
        }
        default:
            break;
    }
    return [[UITableViewCell alloc] init];
}
  • Use your view model TableViewViewModel
#import "TableViewViewModel.h"

@implementation TableViewController {
    TableViewViewModel *viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:HEADER_VIEW_NIB_NAME bundle:nil] forHeaderFooterViewReuseIdentifier:HEADER_VIEW_IDENTIFIER];
    viewModel = [[TableViewViewModel alloc] initWithTableView:_tableView];
    _tableView.dataSource = viewModel;
    [viewModel update];
}

@end

CollectionView

  • Just the same way of TableViewViewModel. Create a new class CustomCollectionViewModel extends TableViewBasicViewModel<UICollectionViewDataSource>, and Import this CustomCollectionViewModel to view controller

  • You can add your init method and register the cell inside

  • For setup items, other words, setup the sections/rows you want to show. Create CustomCollectionSectionItem and CustomCollectionRowItem, CustomCollectionSectionType

  • Override UICollectionViewDataSource

Advanced settings

Methos of TableViewBasicViewModel

  • TableViewBasicViewModel provides some usage methods
- (NSInteger)getRowTypeWith:(SectionType)type row:(NSInteger)row;
- (NSString *)getSectionTitleinSection:(NSInteger)section;
- (UIImage *)getSectionLeftIconinSection:(NSInteger)section;
- (SectionType)getSectionTypeinSection:(NSInteger)section;
- (void)hideRows:(BOOL)hide inSection:(NSInteger)section;
- (BOOL)hiddenRowsinSection:(NSInteger)section;
- (NSIndexSet *)getIndexSetWithSectionType:(SectionType)sectionType;
- (NSIndexPath *)getIndexPathWithSectionType:(SectionType)sectionType rowType:(RowType)rowType;
- (void)setupRowTag;
- (NSIndexPath *)getIndexPathFromRowTag:(NSInteger)rowTag;

Tag

  • Because the cells have reuse feature, somtimes we need to tag the cell/componenst if want to recognize the specific cell/components, thus IRCollectionTableViewModel provides a tag feature

  • Setup tags by setupRowTag, it save the tag information in the tagRange which is in the RowBasicModelItem

- (void)setupRows {
    ...
    
    [self setupRowTag];
}
  • Get tag
TableViewRowItem *row = (TableViewRowItem *)[item.rows objectAtIndex:[indexPath row]];
tag = row.tagRange.location;
  • Get indexPath by tag
- (NSIndexPath *)getIndexPathFromRowTag:(NSInteger)rowTag;
  • Sometimes you want to tag the UI components like UITextField, use setTagRangeLength
TableViewRowItem *row = [[TableViewRowItem alloc] initWithType:RowType_DemoRow withTitle:@"Demo Row"];
[row setTagRangeLength:2];

...

[self setupRowTag];
  • Then get tags
TableViewRowItem *row = (TableViewRowItem *)[item.rows objectAtIndex:[indexPath row]];
tag1 = row.tagRange.location;
tag2 = row.tagRange.location + 1;

cell.textField1.tag = tag1;
cell.textField2.tag = tag2;
  • The tags are mapping to the same index path
[self getIndexPathFromRowTag:tag1] == [self getIndexPathFromRowTag:tag2]

Now, you can easy to tag anyhing you want.

Get Row Type

  • Hide rows for specific setion by (void)hideRows:(BOOL)hide inSection:(NSInteger)section
- (NSInteger)getRowTypeWith:(SectionType)type row:(NSInteger)row;

Get Section Title

  • Get section title which set in the SectionBasicModelItem
- (NSString *)getSectionTitleinSection:(NSInteger)section;

Get Section Left Icon

  • Get section icon which set in the SectionBasicModelItem
- (UIImage *)getSectionLeftIconinSection:(NSInteger)section;

Get Section Type

  • Get section type by section index
- (SectionType)getSectionTypeinSection:(NSInteger)section;

Hide Rows

  • Hide rows for specific setion by (void)hideRows:(BOOL)hide inSection:(NSInteger)section
  • Check hidden status by (BOOL)hiddenRowsinSection:(NSInteger)section
- (void)hideRows:(BOOL)hide inSection:(NSInteger)section;
- (BOOL)hiddenRowsinSection:(NSInteger)section;

Get Index Set

  • Get index of section by section type
- (NSIndexSet *)getIndexSetWithSectionType:(SectionType)sectionType;

Get IndexPath

  • Get index path by section type and row type
- (NSIndexPath *)getIndexPathWithSectionType:(SectionType)sectionType rowType:(RowType)rowType;

Screenshots

TableView CollectionView
TableView CollectionView

ircollectiontableviewmodel's People

Contributors

irons163 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.