3

So basically i have search method "searchTableList" and after i get the result i want, i want to reload the uicollectionview

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
   // NSLog(@"Text change - %d",isSearching);

    //Remove all objects first.
    [filteredContentList removeAllObjects];

    if([searchText length] != 0) {
        isSearching = YES;
        [self searchTableList];
    }
    else {
        isSearching = NO;
    }

    [self.collectionView reloadData];


}

However after "[self.collectionView reloadData]" Nothing happens !

After using this, it crashes all the time !

[self.collectionView reloadItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]];
[self.collectionView reloadData];

What can i do, Thanks alot!!! :D

Error message is:

    2014-04-16 18:24:35.684 SampleProject1[59602:60b] the item width must be less that the width of the UICollectionView minus the section insets left and right values.
2014-04-16 18:24:37.709 SampleProject1[59602:60b] *** Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit_Sim/UIKit-2935.137/UICollectionView.m:3688
2014-04-16 18:24:37.713 SampleProject1[59602:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert item 0 into section 0, but there are only 0 items in section 0 after the update'

Fixed It:

Basically i was returning the count of an unused array which always had a lenght of 0 instead of the relevant array !

So i had to change This:

if (isSearching) {

    return [searchResult count];

} else {
    return [self.tracks count];
}

To: if (isSearching) {

        return [filteredContentList count];

    } else {
        return [self.tracks count];
    }

Then after this i removed all the reloadData jargons and replaced it with:

[self.collectionView reloadData];

Thanks alot guys !!

Daniyar
  • 2,975
  • 2
  • 26
  • 39
ipalibowhyte
  • 1,553
  • 2
  • 22
  • 34

2 Answers2

4
  • After writing reloadItemsAtIndexPaths no need of writing again reloadData.
  • Reload the respective collection View row using main thread.

Code Snippet:

[[NSOperationQueue  mainQueue]  addOperationWithBlock:^{

    // Reload the respective collection view row using the main thread.

    [self.collectionView reloadItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]];

}];
Bista
  • 7,869
  • 3
  • 27
  • 55
Jayaprada
  • 944
  • 8
  • 11
0

Check for any [collectionView reloadData] commands in your willDisplayCell... method. I had this in there and it worked once, but when I needed to reloadData again with a new dataset it just wouldn't trigger any more willDisplayCell calls.

It was all on the main thread, that wasn't the problem. More like an overflow problem.

Doug Voss
  • 1,012
  • 1
  • 14
  • 11