2012-12-17 19 views
6

W pewnych zdarzeń,Jak usunąć wszystkie elementy i dodać nowe elementy do UICollectionView?

Chciałbym usunąć wszystkie komórki w uicollectionview i dodawanie nowych komórek. (Która zostanie wypełniona w wyniku połączenia sieciowego)

Próbowałem za pomocą deleteItemsAtIndexPaths. reloadSections, deleteSections/insertSections.

Każdy z nich zniszczył moją aplikację.

Poniżej znajduje się mój kod.

NSMutableArray* indexPathArray = [[NSMutableArray alloc] init]; 

for(int i=0; i < [self.jsonAlbumImageArray count]; ++i) 
{ 
    NSIndexPath* newPath = [NSIndexPath indexPathForRow:i inSection:0]; 
    [indexPathArray addObject: newPath]; 
} 

[self.jsonAlbumImageArray removeAllObjects]; 

if(indexPathArray.count > 0) 
{ 
    [self.collectionView performBatchUpdates:^{ 
      [self.collectionView deleteItemsAtIndexPaths:indexPathArray]; 
     } 
    completion: nil]; 
} 

// NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)];                                               
// NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:0];                                                     
// [self.collectionView reloadSections:indexSet];                                                       

[self get_IMAGE_LIST_FROM_SERVER]; 
+1

użycie '[CollectionView reloadData]' –

Odpowiedz

4

Tak, jak Jonathan powiedział, myślę, że to, co chcesz zrobić, to zmienić swoje źródło danych z nowymi danymi i po prostu „odświeżyć” lub przeładować UICollectionView.

+0

Co więc mówiąc, jeśli jasne źródło danych całkowicie i zrobić '' '[CollectionView reloadData]' '' aplikacja nie będzie katastrofy, a widok kolekcja będzie wyczyszczone? – Supertecnoboff

+0

jeśli przez "wyczyść źródło danych" masz na myśli uczynienie z niego obiektu zliczającego 0, to tak, właśnie to się stanie. Jeśli jednak sprawisz, że będzie "zero", możesz uzyskać nieoczekiwane wyniki, w zależności od tego, czy używasz metody swift, czy obj-c. –

+1

Niesamowite dzięki Travisowi i tak, to właśnie miałem na myśli :) – Supertecnoboff

6

W przypadku, gdy potrzebujesz płynniejszej animacji niż ta dostarczona przez [collectionView reloadData] możesz to zrobić za pomocą metod wstawiania i usuwania, tutaj jest przykład.

-(void)deleteItemsFromDataSourceAtIndexPaths:(NSArray *)itemPaths 
{ 
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet]; 
    for (NSIndexPath *itemPath in itemPaths) { 
     [indexSet addIndex:itemPath.row]; 
    } 
    [self.apps removeObjectsAtIndexes:indexSet]; 
} 

-(void)insertItems:(NSArray*)items ToDataSourceAtIndexPaths:(NSArray *)itemPaths 
{ 
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet]; 
    for (NSIndexPath *itemPath in itemPaths) { 
     [indexSet addIndex:itemPath.row]; 
    } 
    [self.apps insertObjects:items atIndexes:indexSet]; 
} 

-(void)reloadDataSmooth{ 
     [self.collectionView performBatchUpdates:^{ 

      NSMutableArray *arrayWithIndexPathsDelete = [NSMutableArray array]; 
      NSMutableArray *arrayWithIndexPathsInsert = [NSMutableArray array]; 

      int itemCount = [self.items count]; 

      for (int d = 0; d<itemCount; d++) { 
       [arrayWithIndexPathsDelete addObject:[NSIndexPath indexPathForRow:d inSection:0]]; 
      } 
      [self deleteItemsFromDataSourceAtIndexPaths:arrayWithIndexPathsDelete]; 
      [self.collectionView deleteItemsAtIndexPaths:arrayWithIndexPathsDelete]; 

      int newItemCount = [newItems count]; 

      for (int i=0; i<newAppCount; i++) { 
       [arrayWithIndexPathsInsert addObject:[NSIndexPath indexPathForRow:i inSection:0]]; 
      } 
      [self insertItems:newItems ToDataSourceAtIndexPaths:arrayWithIndexPathsInsert]; 

      [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPathsInsert]; 
     } 
     completion:nil]; 
    } 
} 
+1

Co to jest "self.apps" tutaj? –

+0

@TejaswiYerukalapudi tablica (zmienna) z CollectionCells – Emilio

Powiązane problemy