Skip to content

haifengkao/HFViewBinding

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HFViewBinding

[![CI Status](http://img.shields.io/travis/Hai Feng Kao/HFViewBinding.svg?style=flat)](https://travis-ci.org/Hai Feng Kao/HFViewBinding) Version License Platform

UITableView and UICollectionView Binding

This project privides some helper functions to bind a mutable array to table views or colllection views. It supports single or multiple sections.

Installation

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

pod "HFViewBinding"

Usage

KVOMutableArray* someData = [[KVOMutableArray alloc] 
                        initWithMutableArray:[@[@"cell 1", @"cell 2"] mutableCopy]];

HFTableViewBinding* bindingHelper = 
[HFTableViewBinding bindingForTableView:self.tableView 
                                   sourceList:someData 
                            didSelectionBlock:^(id model) 
                                             { NSLog(@"clicked on %@", model); } 
                        templateCellClassName:@"YourCellClass"
                                     isNested:NO];

bindingHelper.delegate = self;
bindingHelper.dataSource = self;

You have to use KVOMutableArray as the mutable array class to inform the observers about the mutation event. See KVOMutableArray for more details.

The cell must implement HFBindingDelegate protocol.

The delegate and dataSource settings are optional. If they are set, all delegate methods which are not handled by bindingHelper will be sent to self in the above exmaple.

To support multiple sections, isNested should be set to YES. The each item in the array must be the class of KVOMutableArray.

KVOMutableArray* firstRow = [[KVOMutableArray alloc] 
                        initWithMutableArray:[@[@"cell 1", @"cell 2"] mutableCopy]];
KVOMutableArray* secondRow = [[KVOMutableArray alloc] 
                        initWithMutableArray:[@[@"cell 1", @"cell 2"] mutableCopy]];
KVOMutableArray* someData = [[KVOMutableArray alloc] initWithMutableArray:[@[firstRow, secondRow] mutableCopy]];

HFTableViewBinding* bindingHelper = 
[HFTableViewBinding bindingForTableView:self.tableView 
                                   sourceList:someData 
                            didSelectionBlock:^(id model) 
                                             { NSLog(@"clicked on %@", model); } 
                        templateCellClassName:@"YourCellClass"
                                     isNested:YES];

Motivation

MVVM (Model-View-ViewModel) is a popular replacement of original MVC architecture. It moves the UI logics from hard-to-test Controller to testable ViewModels. MVVM relies heavily on view bindings. But the available binding libraries (HRTableCollectionBindingHelper, CETableViewBinding) don't support table view with multiple sections. Besides, the table view animations of insertion and deletion are disabled in their implmentation, which really annoys me :(

Credit

The APIs are referenced from HRTableCollectionBindingHelper.

The idea of delegate forwarding comes from CETableViewBinding.

Big thanks to Rannie and Colin.

Requirements

Requires iOS 7.0, and ARC.

Author

Hai Feng Kao, haifeng@cocoaspice.in

Contributing

Forks, patches and other feedback are welcome.

License

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

About

bind UITableView or UICollectionView to an array in MVVM architecture

Resources

License

Stars

Watchers

Forks

Packages

No packages published