0

I am trying to address a requirement that displays dynamically generated content (a travel itinerary with a 'run time computed' number of legs) within a scrollable area in a custom UITableView cell.

In brief, I directly add itinerary information in the form of view into a UIScrollView.

The basic custom cell layout, a view with some personal information above a UIScrollView is laid out in a xib. The itinerary information is directly written into another view laid out in another xib. These multiple itinerary leg views are then added as subviews to the scrollview.

Therein lies the problem.

Since the cell is reused, old data, in this case itinerary record fragments from other records can appear at the end of the scroll view. I attempt to fix this by reseting the scroll view when the custom cell is loaded:

 - (void)viewWillAppear:(BOOL)animated
 {
     [_scroller.subviews 
          makeObjectsPerformSelector:@selector(removeFromSuperview)];
     [_scroller setContentOffset:CGPointMake(0,0) animated:YES];
 }

I derived this from

Clear content of UIScrollView

This doesn't appear to work in my case.

To answer a few questions in advance, I am aware of other ways to accomplish similar results, and in fact use them in this app, such as answer #1 here (like I said, I am trying to address a requirement):

adding UITableView inside UITableViewCell

and I have read numerous discussions concerning the suitability (or non-suitability) of

 -(void)prepareForReuse

Surely this problem has a well-known solution.... I just can't seem to find it.

Any assistance greatly appreciated.

jmf1205
  • 437
  • 6
  • 23
  • Use `prepareForReuse` and remove the subviews. Pretty straight-forward. – Paulw11 Jun 11 '17 at 06:04
  • That fixes it, I didn't originally try this as the Apple documentation seems to imply that this isn't correct usage: "For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. " – jmf1205 Jun 11 '17 at 06:36
  • If you make this into an answer, I will accept it for you. – jmf1205 Jun 11 '17 at 07:26

0 Answers0