Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Loop Through Cells in UITableView

Hey Guys!

So I'm working on a project right now involving a UITableView. I have the cells' alpha initially set to 0 and want each cell to appear about 1 second after the other. I imagine I would need to loop through the array of tableviewcells. Any ideas? Amit Bijlani

Thanks!

2 Answers

hello greg,

what I would do is when you create the cells (in cellForRowAtIndexPath) set the cells alpha to 0 then set it to one in an animation block, it should look something like this

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    cell.alpha = 0;

    [UIView animateWithDuration:1 animations:^{
            cell.alpha = 1.0;
        }];
}

this is the first thing that comes to mind, let me know if it works.

Best, Kai.

Thanks, while that does work, it's not what I'm looking for. That code makes all the cells in the tableview fade in at the same time - I need each cell to fade in about 0.5 to 1 second after the previous.

aha i thought you might say that so I thought of doing something like this

[UIView animateWithDuration:1.0
                          delay:indexPath.row
                        options:UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         cell.alpha = 1.0
                     }completion:nil];

try that and let me know if it works

Let me know if this works, Kai

this might or might not work, if it doesn't try something like

//add all cells to a mutable array called "allCells"

-(void)animationMethod{
    int i = 0;
    [UIView animateWithDuration:1.0 animations:^{
        NSIndexPath *indexPath = [[NSIndexPath alloc]initWithIndex:i];
        [self.collectionView cellForItemAtIndexPath:indexPath].alpha = 1.0;
    }completion:^{
        while (i < allCells.count) {
            i++
            NSIndexPath *otherIndex = [[NSIndexPath alloc]initWithIndex:i];
            [UIView animateWithDuration:1
                                  delay:1
                                options:UIViewAnimationOptionAllowUserInteraction
                             animations:^{
                                    [self.collectionView cellForItemAtIndexPath:otherIndex].alpha = 1.0;
                                }completion:nil];
        }
    }];
    }
}

this might work, anyways call this method once all cells are made,

Best, Kai

Thank you so much! The second answer works fantastic!

No problem :)