当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Change UINavigationbar title according to UICollectionView Cell

问题描述:

Im trying to change the title of UINavigationbar title according to UICollectionView cell indexPath.row, I have horizontal UICollectionView cells like Photo sliding-content (as Photo app in IOS).

What i am trying to achieve is when i move from indexPath.row for example from 1 to 2 to change the title but i am having one issue, I've placed the following code in cellforitematindexpath or willdisplaycell :

self.title = "\(indexPath.row+1) of 2"

This code works fine but it has one issue, which is when i start dragging to the next cell but i return to where am i, the title changed to be 2 but i am still in 1 ! which is really annoying !

I've tried also to place the code in scrollViewDidEndDecelerating but it has the same issue, like i started dragging but i changed my mind to stay on cell 1 so the method says okey its end dragging, then the title again changes to 2 but i am still in one.

I need to since if i am still where am i then i don't have to change the title, if i am completely in the next cell then change the title.

网友答案:

It was happening because method cellForItemAtIndexPath was called to prepare second item, but first cell is still in memory and method cellForItemAtIndexPath was not called when you return to first. Try this:

-(void)scrollViewDidEndDecelerating:(nonnull UIScrollView *)scrollView {
        float currentPage = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
        NSLog(@"Current page: %f", ceil(currentPage));
}

Don't forget to add UIScrollViewDelegatein your CollectionViewController.h

分享给朋友:
您可能感兴趣的文章:
随机阅读: