IOS: UICollectionView inside UIScrollView index does not grow

Hi stackoverflow folks,

Currently working on an application where I have a user profile that looks like this

http://i.imgur.com/H1N0ouX.jpg (sorry I don't have enough reputation to post the image)

The header view is the user's image. Tab Tabs are two tabs, one with a collection and one with a table.

All of these views are nested in scrollView

My problem is that when I view this view (the collection set height is set programmatically based on the number of items), the collection view index does not increase. Only the first 6 items are loaded into the collection view's visible cells index.

I need to navigate from collection items to item detail view.

How can I use the scrollView delegate to find out where my collection view is.

I've looked at these topics: iOS 7 Collection View Inside Scroll> How do I implement a scrollable UICollectionView inside a UIScrollView?

But nobody gave me the answer I was looking for.

I also tried looking at scrollViewDideEndDecelerating and setting the offset of the scroll view as a collection according to the offset of the scroll view, but I couldn't get it to work.

Any advice?

thank

EDIT:

Hi and thanks for your answer,

To answer your question, I can scroll my collection view, since it is inside the scroll, I set the height of my view during viewdidload (depending on the number of items).

My problem: I can click on the first 6 items and display the details (via segue), but after that the selection won't be recognized as the index is not updated for my collection view. I can scroll all the way to the bottom of my collection view (scrolling through the scrollView).

My collectionView has user interaction enabled, as I said, I can select the first 6 items and then the selection is not recognized.

I understand the UICollectionViewFlowLayout problem, but where am I writing this line? in viewDidLoad? or in the CollectionView delegate functions?

If you need more information, I can copy the code or show the view layout in the storyboard

Thanks in advance.

EDIT 2:

images, storyboard: layout of my view

http://i.gyazo.com/c491786507db2effc702d910020515a2.png

here are the collectionView datasource functions

http://gyazo.com/3730caeb2b2a40fef9efec559171744f

delegate function in collection view

http://i.gyazo.com/81fc9443b4367ecbd304e608ab0cc864.png

EDIT3:

Ok so this is basically what I want to reproduce,

tab in the middle with short tabs with collection view, it all scrolls. What I cannot figure out is if I set my topView as the title of the CollectionView, I cannot switch tabs as I want, starting from it inside the title.

ViewController with TalbeView and CollectionView tabs like Google+ on IOS

+3


source to share


2 answers


Hey. First of all, I need to go into more detail on your question, but I am assuming that you want your UICollectionView to scroll and not actually scroll.

So, you could check the following points: -

  • UserEnableInteraction must be enabled to view the collection.

  • And also check what value you gave for the UICollectionViewFlowLayout because just think if you have only 5 items and your UICollectionViewFlowLayout size is like it can display all five items in the visible index of the cell then it doesn't scroll so you you need to increase the size of the UICollectionViewFlowLayout for it to scroll. eg



UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];

[flowLayout setItemSize:CGSizeMake(148, 148)];

      

  1. And if you want you can get help from the UIScrollViewDelegate method like

    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
    if (!decelerate)
    {
        // do your job
    }
    }
    
          

0


source


set delegates and datasource to .h

#import <UIKit/UIKit.h>
 @interface ViewController : UIViewController<UITableViewDataSource , UITableViewDelegate ,         
  UIScrollViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout>

      

You can do this .m

in ViewDidLoad

: -



    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    UICollectionView *collectionView =[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout ];
    collectionView.frame = CGRectMake(5, 61, 365, 678);
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [self.view addSubview:collectionView];

      

And then Call her delegates method: -

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

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

    [self fillCell:cell cellForRowAtIndex:indexPath];
     return cell;
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return CGSizeMake(50, 50);
//}

      

-2


source







All Articles