Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reload a Collection View automatically?

I have developed an RSS reader application using collection views. My problem is, on the very first launch, the app comes up blank.

The process is, the RSS feed is pulled from the web, stored to a temporary file on the device and then displayed via collection views.

My question is How Do i get the app to reload the data automatically once the files have been loaded? So that the user does not see an initial blank screen.

I have tried adding this code

       [self.collection1 performBatchUpdates:^{
       [self.collection1 reloadSections:[NSIndexSet indexSetWithIndex:0]];
   } completion:nil];

However it does not work for the initial view.

Should I do something from the appdelegate? or from the collection view controller itself?

Please advise

Below is the code used to get and display my data....

#pragma mark - UICollectionViewDataSourceDelegate
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  {
  return 1;
  }

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
 return [_articleListmain count];
}

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
 MainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"articleCellmain"
                                                                           forIndexPath:indexPath];
NSDictionary *item = [_articleListmain objectAtIndex:indexPath.item];

// set the article image
[cell.image setImageWithURL:[item objectForKey:@"image"]];

// set the text of title UILabel
cell.title.text = [NSString stringWithFormat:@"%@\n\n\n\n", [item objectForKey:@"title"]];
cell.title.textColor = [UIColor colorWithRed:33.0f/255.0f green:74.0f/255.0f blue:146.0f/255.0f alpha:1.0f];

// set the text of summary UILabel
cell.description.text = [NSString stringWithFormat:@"%@\n\n\n\n\n\n\n\n\n", [item objectForKey:@"description"]];


cell.targetURL.text = [NSString stringWithFormat:@"%@\n\n\n\n", [item objectForKey:@"link"]];

cell.category.text = [NSString stringWithFormat:@"%@\n\n\n\n", [item objectForKey:@"category"]];

cell.date.text = [NSString stringWithFormat:@"%@\n\n\n\n", [item objectForKey:@"pubDate"]];

cell.image.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"placement.png"]];


return cell;


}
like image 491
Robert Farr Avatar asked Sep 23 '13 23:09

Robert Farr


People also ask

How do you refresh data in collection view?

reloadData() Reloads all of the data for the collection view.

How do I know that the Uicollectionview has been loaded completely?

Simply reload collectionView inside batch updates and then check in the completion block whether it is finished or not with the help of boolean "finish".

How do I register a custom collection view cell?

Go to the storyboard and select the cell widget. Set the class to be the newly created class which is CustomCollectionViewCell in this case. While we have the cell selected, set the reuse identifier to cell . This reuse identifier will be referenced when customizing the cell in the CustomCollectionView.


2 Answers

You should be able to use [collectionView reloadData] once you know the download has completed.

like image 190
RegularExpression Avatar answered Nov 15 '22 00:11

RegularExpression


try this

 [self.collection1 performBatchUpdates:^{
   [collectionView reloadData]
 } completion:nil];

once your download has completed.

like image 29
Bhushan Rana Avatar answered Nov 14 '22 22:11

Bhushan Rana