title: Updating a ScrollView's Content
body: <p>I have an app which contains a scrollview with several tableviews. Each tableview is loaded from another viewcontroller. It is built from the PageControl sample app from Apple. My goal is to have it work exactly like Apple's weather app.</p>
  
  <p>I have everything working just fine. All content loads perfectly and scrolling side to side displays all the proper tableviews and their relative data. I have a button which when clicked it opens up another view which allows you to edit each <em>item</em>, just like the weather app where you can add a new city, delete it, or move it around.</p>
  
  <p>The problem I am having is how do I update the scrollview when the user is done editing the <em>items</em>. Imagine in the PageControl app being able to remove page 5 or move page 4 into position #1, etc.</p>
  
  <p>I haven't pasted any code because: 1) it's the same code in the PageControl app; 2) I haven't figured out where to start. Hopefully someone here can help me.</p>
  
  <p>Thanks.</p>
  
  <p><strong>UPDATE: (3/5/2010 3:18AM EST)</strong></p>
  
  <p>Ok, so I been working on this for a while. I was able to call a method in the mainView to update he scrollView. The code seems kinda clunky, but it works! I really do not like the code because I end up using a property once, and I cannot set it again, because I get an error <code>objc[10801]: FREED(id): message release sent to freed object=0x3f4a490</code>. To me it seems like the object has been release? If so, I don't know how, as I only release it in the <code>dealloc</code>. <code>dealloc</code> is never called (I put an <code>NSLog</code> to check) so I don't know what's going on.</p>
  
  <p>Code: The initial code is the same code from PageControl sample app from Apple:
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
      appDel = (iBarryAppDelegate *)[[UIApplication sharedApplication] delegate];
      managedObjectContext = appDel.managedObjectContext;
      NSMutableArray *controllers = [[NSMutableArray alloc] init];
      for (unsigned i = 0; i &lt; numberOfPages; i++)
      {
          [controllers addObject:[NSNull null]];
      }
      self.viewControllers = controllers;
      [controllers release];
      scrollView.pagingEnabled = YES;
      scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * numberOfPages, scrollView.frame.size.height);
      scrollView.showsHorizontalScrollIndicator = NO;
      scrollView.showsVerticalScrollIndicator = NO;
      scrollView.scrollsToTop = NO;
      scrollView.delegate = self;
      pageControl.numberOfPages = numberOfPages;
      pageControl.currentPage = 0;
      //I end up commenting out all the method calls to
      //[self loadScrollViewWithPage:xxx]; and load all the views at once.
      //I know I should be lazy-loading here , but Scrolling is much 
      //faster if I load them all at once, instead 
      //of when they are about to be viewed.
      for (unsigned i = 0; i &lt; numberOfPages; i++)
      {
          [self loadScrollViewWithPage:i];
      }
  }
  - (void)loadMainController:(int)page
  {
      MainViewController *controller = [viewControllers objectAtIndex:page];
      if ((NSNull *)controller == [NSNull null])
      {
          controller = [[MainViewController alloc] initWithPageNumber:page];
          [viewControllers replaceObjectAtIndex:page withObject:controller];
          [controller release];
      }
      if (nil == controller.view.superview)
      {
          CGRect frame = scrollView.frame;
          frame.origin.x = frame.size.width * page;
          frame.origin.y = 0;
          controller.view.frame = frame;
          [scrollView addSubview:controller.view];
      }
  }
  - (void)loadScrollViewWithPage:(int)page
  {
      if (page &lt; 0) return;
      if (page >= numberOfPages) return;
      [self loadMainController:page];
  }
  - (void)scrollViewDidScroll:(UIScrollView *)sender
  {
      if (pageControlUsed)
      {
          return;
      }
      CGFloat pageWidth = scrollView.frame.size.width;
      int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
      pageControl.currentPage = page;
  //  [self loadScrollViewWithPage:page - 1];
  //  [self loadScrollViewWithPage:page];
  //  [self loadScrollViewWithPage:page + 1];
  }
  - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  {
      pageControlUsed = NO;
  }
  - (IBAction)changePage:(id)sender
  {
      int page = pageControl.currentPage;
  //  [self loadScrollViewWithPage:page - 1];
  //  [self loadScrollViewWithPage:page];
  //  [self loadScrollViewWithPage:page + 1];
      CGRect frame = scrollView.frame;
      frame.origin.x = frame.size.width * page;
      frame.origin.y = 0;
      [scrollView scrollRectToVisible:frame animated:YES];
      pageControlUsed = YES;
  }</pre></code></p>
  
  <p>I then have my code to <strong><em>refresh</em></strong> the scrollView. What I do is remove all the subviews and load them again. I won't ever have more than 13 subViews, not sure if deleting them all and recreating them is a good or bad idea.</p>
  
  <pre><code>- (void)reloadDataNow
  {
      for(UIView *subview in [scrollView subviews]) {
              [subview removeFromSuperview];
              NSLog(@"removed a suview");
      }
      [self loadData]; //this method fetches which data will be 
      //in the scrollView. It reloads the number of pages based 
      //on the return result and an NSMutableArray where I store 
      //data to pass to the views that will appear in the scrollView
      NSMutableArray *controllers = [[NSMutableArray alloc] init];
      for (unsigned i = 0; i &lt; numberOfPages; i++)
      {
          [controllers addObject:[NSNull null]];
      }
      scrollView.pagingEnabled = YES;
      scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * numberOfPages, scrollView.frame.size.height);
      scrollView.showsHorizontalScrollIndicator = NO;
      scrollView.showsVerticalScrollIndicator = NO;
      scrollView.scrollsToTop = NO;
      scrollView.delegate = self;
      pageControl.numberOfPages = numberOfPages;
      pageControl.currentPage = 0;
      for (unsigned i = 0; i &lt; numberOfPages; i++)
      {
          MainViewController *controller = [controllers objectAtIndex:i];
          if ((NSNull *)controller == [NSNull null])
          {
              controller = [[MainViewController alloc] initWithPageNumber:i team:[[favoritesArray objectAtIndex:i] teamR]];
              [controllers replaceObjectAtIndex:i withObject:controller];
              [controller release];
          }
          if (nil == controller.view.superview)
          {
              CGRect frame = scrollView.frame;
              frame.origin.x = frame.size.width * i;
              frame.origin.y = 0;
              controller.view.frame = frame;
              [scrollView addSubview:controller.view];
          }
      }
      //********************CRASH******************
      //Ideally, I think, the line below would replace the contents of
      //viewControllers with the new controllers. Here is where I get the 
      //"objc[10801]: FREED(id): message release sent to freed object=0x3f4a490" 
      //error. This line should probably be placed after [self loadData] or something,
      //but it crashes so I put but here to display better. I tried
      //adding self.viewControllers = nil; but I don't think that is 
      //the problem with the error message.
      self.viewControllers = controllers;
  }</code></pre>
  
  <p>The <code>reloadDataNow</code> method is called from a child view where one can add or remove <em>items</em> to show on the scrollView.</p>