Git Product home page Git Product logo

cetableviewbinding's Introduction

ReactiveCocoa TableView Binding Helper

This project contains a simple helper class that can be used to bind array properties on ReactiveCocoa view models to table views. Here's a quick example of how to use it:

// create a cell template
UINib *nib = [UINib nibWithNibName:@"CETweetTableViewCell" bundle:nil];

// bind the ViewModels 'searchResults' property to a table view
[CETableViewBindingHelper
     bindingHelperForTableView:self.searchResultsTable
                  sourceSignal:RACObserve(self.viewModel, searchResults)
              selectionCommand:nil
                  templateCell:nib];

In the above example, the nib CETweetTableViewCell defines a UITableViewCell subclass which is used for rendering the items within the array. This cell must implement the CEReactiveView protocol.

The binding helper binds a source signal, which is a signal constructed from the array property of the view model, to a UITableView. As a result, the table view will render the contents of the array using the given cell template. Updates to the array property on the view model are automatically be propagated.

If you need to handle selection, you can supply a RACCommand to the binding helper via the selectionCommand argument. This command is executed each time selection changes.

If you need to mutate (i.e. add / remove items) the array property of the view model, use the CEObservableMutableArray class. This is an NSMutableArray subclass that informs observers of mutations, allowing the binding helper to add / remove rows from the table view automatically. See the QuotesListExample project for an example of binding to mutable arrays.

This project contains two example projects:

  • TwitterSearchExample - An example app that searches twitter, with a table view, bound using the helper, displaying the results.
  • QuotesListExample - An example app that shows a list of stock quotes, where prices are dynamically updated and items are added / removed.

To learn more about this helper, see the following blog posts:

cetableviewbinding's People

Contributors

bryant1410 avatar colineberhardt avatar pitometsu avatar thibault-carpentier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cetableviewbinding's Issues

tableView:viewForHeaderInSection: never fires

I've tried :
· Returning a positive value from tableView:heightForHeaderInSection:
· Setting _tableView.sectionHeaderHeight to a positive value.
· Returning a non-nil value from tableView:titleForHeaderInSection:
· Calling -[UITableView registerClass:forHeaderFooterViewReuseIdentifier:]

Under no circumstance is tableView:viewForHeaderInSection: called.
Other delegate methods are firing. To be clear, the problem is not simply that the message is not being forwarded (which is all the implementation of UITableViewDelegate protocol does in the helper), but rather that the method in the helper does not fire.

Not sure if it's relevant but the helper is instantiated in a UITableViewController which is set up in code, not Storyboard.

Thanks for the code, I'm really enjoying working with it and I thought the associated blog posts were excellent.

How to forward UITableViewDelegate methods?

I have created property in "CETableViewBindingHelper.h" file.

@Property (weak, nonatomic) id delegate;

Now how to use this delegate to forward "didSelectRowAtIndexPath" method to view controller?

I have implemented below code in "CETableViewBindingHelper.m" file.

 #pragma mark = UITableViewDelegate methods

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

      // execute the command
      [_selection execute:_data[indexPath.row]];

      // forward the delegate method
      if ([self.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
           [self.delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
     }
}

But "[self.delegate tableView:tableView didSelectRowAtIndexPath:indexPath];" this line is not executed.

Can you please help me that how can I forward any tableview delegate methods in my ViewController.

Support for Multiple Sections

Love this library, it was very cool to take it apart piece by piece and see how it all worked.

I noticed though that it doesn't seem to have support for multiple sections in a table view (given that numberOfSectionsInTableView: isn't implemented) or multiple subclasses of UITableViewCell since just one template cell is passed to the init function. I built an analogous helper class for UICollectionView and I added in support for what I just mentioned. However, what I'm stuck on the most and hoped that you could give some insight into, is how to model the data.

In CETableViewBindingHelper, the data property is just an NSArray so it's easy to index right into that array and it seems like any table view that would use this project can only have one section in it, which makes sense since it seems more of a fun example project (albeit a kickass one).

So the question is, what is the best way to model the data provided to the helper class so that it's 1) easy for the user to populate information and 2) maintains some level of abstraction.

The easiest and most obvious way I can think of is have a CEObservableMutableArray instance that has a dictionary in each spot. This dictionary would look something like:

@{@"header":someHeaderModel, @"footer: someFooterModel, @"items:@[@{@"reuseIdentifier":@"REUSE", @"models":@[anObject, anObejct2] } }

but to me that seems to cumbersome from the perspective of the end user.

My next approach was to build an abstract class hierarchy that went like this:

BaseModel
@property (nonatomic) NSArray *sections;

Each spot in section is an instance of a SectionModel class which would look like:

SectionModel
- dictionary of supplementary views indexed by kind
- NSArray of items

Each item would then be an instance of ItemModel

ItemModel
- NSString *reuseIdentifier

  • id viewModel (this is what gets passed into bindViewModel:

A better way to go about it would be to remove BaseModel completely and have a list of SectionModel's but both ways still present the same problem; there seems too much burden on the end user of the software to construct each ItemModel and SectionModel themselves.

The ultimate goal of course is to have the binder be able to dequeue cells and supplementary views in an easy and consistent way such as self.data[indexPath.section][indexPath.row] and I think that goal somewhat informs the decision to make an abstraction over the data so that the user can only provide say an array of SectionModels and in that way, the helper would be able to support it. If you have any suggestions as to my quandary, it would be much appreciated.

retain cycle?

 [source subscribeNext:^(id x) {
      if ([x isKindOfClass:[CEObservableMutableArray class]]) {
        ((CEObservableMutableArray *)x).delegate = self;
      }
      if (self->_data != nil && [self->_data isKindOfClass:[CEObservableMutableArray class]]) {
        ((CEObservableMutableArray *)self->_data).delegate = nil;
      }
      self->_data = x;
      [self->_tableView reloadData];
    }];

I just wondering is this make retain cycle? that why you dont need to set the Binding class into some public property right? since tableview.delegate is weak reference

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.