Saraswathi Saraswathi - 1 month ago 11
Objective-C Question

CollectionView Should scroll to last cell

I am using collection view to showing the taken images of my app.I want as soon as the image taken the collection view should automatically move to last object in my collection view .My collection scrolls horizontally
Please help me to do this.

I have searched in google and i have found i have applied this code in my project

i have applied this code in my project in viewwillappear ,it showing error,
Please help me to do this

click here to see i am taking photo and showing in below ,ok ,in this image it has 3pictures ,if i again take a picture 4th picture will be there in collection view ,if user want to se the 4th picture means ,user want scroll by manually and can see it...

Here is my code:

-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[self navigationController] setNavigationBarHidden:NO animated:YES];
[session startRunning];
NSInteger section = [self numberOfSectionsInCollectionView:self.collection_View] - 1;
NSInteger item = [self collectionView:self.collection_View numberOfItemsInSection:section] - 1;
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];
}


I want automatically scroll to the last pictures in horizontally
Thanks in Advance !!!

Answer

Add the following line inside your viewWillAppear method code:

[collectionView scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];

Use this:

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[self navigationController] setNavigationBarHidden:NO animated:YES];
    [session startRunning];
    NSInteger section = [self numberOfSectionsInCollectionView:self.collection_View] - 1;
    NSInteger item = [self collectionView:self.collection_View numberOfItemsInSection:section] - 1;
    NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];

    [collectionView scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
}