23

Now that there's a full support for KVO, has anybody made a mutable table view that takes a RACSignal as its dataSource? Ideally something that doesn't require any configuration.

RACSignal *commentsSignal;
UITableView *table = [UITableView new];
table.dataSourceSignal = commentsSignal;
[self.view addSubview:table];
// No more basic config
iamVishal16
  • 1,780
  • 18
  • 40
Michael
  • 1,212
  • 1
  • 14
  • 17
  • 1
    Content of the table view is not just a collection of objects, but there is much more: heights, sections, titles, custom cell, … So you will have to do much more to fully configure the table view. – Tricertops Jul 03 '13 at 17:55
  • Af you are asking only for reloading, insertions, deletions and moving of cells, then this is what I usually do, but again: every table is so different, that I didn't find universal case for this. – Tricertops Jul 03 '13 at 17:59
  • Sorry, this question was about data and collection observers, not appearance. Using ReactiveCocoa, Nimbus, and something like [CueTableReloader](https://github.com/Cue/CueTableReloader), you could make a table view that automatically rearranges itself when you change a collection of models, all in about 4 lines of code. – Michael Jul 03 '13 at 19:34
  • (Off topic: I don't know what do you mean.) – Tricertops Jul 04 '13 at 06:31
  • I will try to put together universal code for managing data source automatically using RAC. May be usefull for many people. – Tricertops Jul 04 '13 at 06:32

2 Answers2

2

ReactiveCocoa 3.0 (currently in development) adds a category on UITableView that does just that.

I haven't updated it in a couple weeks, but I made an early podspec for it: https://gist.github.com/adlai-holler/ae321c3398d7db9a55c0

Adlai Holler
  • 830
  • 7
  • 10
2

Yes, I have created a 'binding helper' that binds a table view to a signal:

http://www.scottlogic.com/blog/2014/05/11/reactivecocoa-tableview-binding.html

You can use it to bind a signal to table view where the cell is defined in a nib, as shown below:

// 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)
                        templateCell:nib];

In the above example the table view is bond to an NSArray property on a view model via RACObserve(self.viewModel, searchResults), however any RACSignal that emits an array will bind just fine.

ColinE
  • 68,894
  • 15
  • 164
  • 232
  • I was looking for something that fully supports mutable data, so I haven't upvoted your answer. I also declined Holler's answer because I can't find the category he's talking about and RC 3.0 is not released yet. – Michael Sep 15 '14 at 18:02
  • no problem - you mention you are looking for something that "fully supports mutable data". I'm just wondering if you could expand on that a little? What specific scenario do you want to support? – ColinE Sep 15 '14 at 20:23
  • Both addition and removal from the source array should cause appropriate table cell animations. Mixed bulk addition/removal should also call `beginUpdates` and `endUpdates` on the table. – Michael Sep 16 '14 at 17:58
  • @Michael, I have created a binding helper similar to Colin's, but it supports appropriate table cell animations. [HFTableCollectionBindingHelper](https://github.com/haifengkao/HFTableCollectionBindingHelper) – Hai Feng Kao Jun 05 '15 at 09:52
  • I have rename the project to [HFViewBinding](https://github.com/haifengkao/HFViewBinding). Shorter name is easier to type. – Hai Feng Kao Aug 19 '16 at 10:05