1

I have 6 cells in a UICollectionView.I would like to change the images in each UICollectionViewCell after a particular interval.How is it possible?

Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
Akhil Jiji
  • 185
  • 1
  • 2
  • 13

4 Answers4

2

You can do it like this:

[NSTimer scheduledTimerWithTimeInterval:10.0f  
                                 target:self 
                               selector:@selector(updateImage:) 
                               userInfo:nil 
                                repeats:YES];

- (void)updateImage
{
     [yourImageview setImage:[UIImage imageWithName:@"image1"]];
     [yourCollectionView reloadData];
}
Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56
chirag bhalara
  • 203
  • 1
  • 10
  • Can I do something using tag? – Akhil Jiji Jan 05 '16 at 09:16
  • *yes you can but collectionview reload is best way * Here is an example to find image view with tag for (UIImageView *imgView in collectionView.subviews) { if ([imgView isKindOfClass:[UIImageView class]]) { if(imgView.tag == 1//any tag which you want) [imgView setImage:[UIImage imageWithName:@"image1"]]; } } – chirag bhalara Jan 05 '16 at 09:48
0

Reload the UICollectionView. It will call delegate methods of CollectionView & write your logic over there.

Nilesh Patel
  • 6,318
  • 1
  • 26
  • 40
0
//Write this to viewDidLoad

self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(AutomaticScroll) userInfo:nil repeats:YES];

//Add this method to the class 

-(void)AutomaticScroll
{
    if (i<[imgArray count])
    {
        NSIndexPath *IndexPath = [NSIndexPath indexPathForRow:i inSection:0];
        [self.collectionView scrollToItemAtIndexPath:IndexPath
                                         atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        i++;
        if (i==[imgArray count])
        {
            i=0;
        }
    }
}
psk
  • 162
  • 1
  • 13
0

3 Steps:

  1. Set a Timer
  2. Shuffle Array
  3. Refresh Collection View

Step 1 : Set a Timer

[NSTimer scheduledTimerWithTimeInterval:2.0f  
                             target:self 
                           selector:@selector(shuffleView:) 
                           userInfo:nil 
                            repeats:YES];

Step 2: Shuffle Array

Here I found a nice solution on how to shuffle the array using Category:

What's the Best Way to Shuffle an NSMutableArray?

Step 3: Reload Collection View

- (void)shuffleView
{
     [MyArray shuffle];
     [yourCollectionView reloadData];
}
Community
  • 1
  • 1
Bista
  • 7,869
  • 3
  • 27
  • 55