title: Reloading CollectionView does not clear previously loaded cells
body: <p>I have an iOS app that utilizes RestKit 0.20.1 to retrieve data from a Restful web service.  I should also add the app uses CoreData.  When the app is started the main screen is a collection view that is populated by a default search term.  There is also a textfield in the header that is used as a search bar.  </p>
  
  <p>I am having trouble clearing out the previously loaded cells when the user uses the search bar.  It just loads the new cells and pushes the previous ones down.  Here is the applicable code.</p>
  
  <pre><code>- (BOOL) textFieldShouldReturn:(UITextField *)textField {
  
  //This sets up the NSDictionary for the Restkit postObject parameters
      NSArray *objects =[NSArray arrayWithObjects:textField.text, nil];
      NSArray *keys =[NSArray arrayWithObjects: @"query",nil];
      NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
      self.search=params;
  
  //This is the POST request to the server
      [[RKObjectManager sharedManager] postObject:nil path:@"/rest/search?ip=255.255.255.0" parameters:search success:nil failure:nil];
  
  //This is what I thought would clear out the old and replace with the new    
      [self.collectionView reloadData];    
  
      [textField resignFirstResponder];
      return YES; 
  }
  </code></pre>
  
  <p>I referenced this question <a href="https://stackoverflow.com/questions/13914080/how-to-remove-all-items-and-add-new-items-to-uicollectionview?rq=1">How to remove all items and add new items to UICollectionView?</a> and <code>[collectionView reloadData]</code> was the accepted answer.</p>
  
  <p>I chose the <code>textFieldShouldReturn:</code> method from <a href="http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12" rel="nofollow noreferrer">this</a> tutorial.
  I have also referenced <a href="https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CreatingCellsandViews/CreatingCellsandViews.html#//apple_ref/doc/uid/TP40012334-CH7-SW7" rel="nofollow noreferrer">Inserting, Deleting, and Moving Section Items</a> in the apple developer library but I'm not quite sure how to implement the delete  items methods.</p>
  
  <p>Any help would be great.  This is clearly a rookie question so code snippets are VERY helpful.</p>
  
  <p><strong>UPDATE:</strong>
  Here is how I got it to work.</p>
  
  <p>Added a call to the <code>deleteAllEntitiesForName</code> method shown below before the <code>postObject</code> method and <code>[self.collectionView reloadData]</code> statements in the code above. </p>
  
  <pre><code>- (void) deleteAllEntitiesForName:(NSString*)entityName {
      NSManagedObjectContext *moc = [self managedObjectContext];
      NSEntityDescription *entityDescription = [NSEntityDescription
      entityForName:entityName inManagedObjectContext:moc];
      NSFetchRequest *request = [[NSFetchRequest alloc] init];
      [request setEntity:entityDescription];
      NSError *error = nil;
      NSArray *array = [moc executeFetchRequest:request error:&amp;error];
      if (array != nil) {
          for(NSManagedObject *managedObject in array) {
              [moc deleteObject:managedObject];
          }
          error = nil;
          [moc save:&amp;error];
      }
  
  }
  </code></pre>
  
  <p>I got it from here: <a href="https://stackoverflow.com/questions/4910063/correct-way-to-refresh-core-data-database">Correct way to refresh Core Data database</a></p>