-
-
Save bunnyhero/9988574 to your computer and use it in GitHub Desktop.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
UITableViewCell *cell = | |
[tableView dequeueReusableCellWithIdentifier:REUSABLE_CELL_ID]; | |
UILabel *label = (UILabel *)[cell viewWithTag:VIEW_TAG]; | |
Model *someModel = [self getModelFromIndexPath:indexPath]; | |
// `takeUntil:` makes the RACObserve() signal complete (and thus breaks the subscription) | |
// when the cell is recycled. | |
RAC(label, text) = [RACObserve(someModel, someKey) | |
takeUntil:cell.rac_prepareForReuseSignal]; | |
return cell; | |
} |
Thank a lot!
really useful code snip!
thx~
Great!!!
How it would be the in swift?
Thanks!
I found a solution with the REX extensions
myLabel.rex_text <~
myViewModel.myLabelValue.producer.take(until: self.rex_prepareForReuse)
I'm still getting errors using this approach: I'm trying to bind a UITextField's signal to a property in my viewModel.
After tracking when each cell calls "prepareForReuse" and when the tableView delegate calls "cellForRowAtIndexPath", it appears that the "prepareForReuse" method is not called nearly as often as the "cellForRowAtIndexPath" method. Since the binding happens within "cellForRowAtIndexPath", this means that another binding is often attempted on a previously bound property on the viewModel (before the cell gets reused), leading to an assertion failure.
I've tried to manage the binding calls myself by adding a "bound" boolean variable to my viewModel that gets set to true after each binding and only gets set to false when the relevant cell gets reused. I only bind when bound == false. This appears to work 80% of the time, but after some heavy scrolling and pushing the cells off the screen, the assertion error shows up again.
Do you know if there's any way around this?
@CharlesW95 did you find a solution please ?
Yup.