0

I have a small problem ..

For convenience and to make you understand better how to share the collection View:

Collectionview 1 has the customized segment control function (it has a custom UICollectionView custom class)

Collectionview 2 contains the cells that function as pages (it has a UICollectionViewController class).

  1. I have an UIViewController that contains the collectionview1 that I used to create a custom segment control (like a menu)

  2. The cursor indicating which cell was selected was created with a uiview in the collectionView 1 class.

  3. In my main view controller in addition to having a collectionview I also inserted a container view that contains a collectionview controller that will be used to show the selected pages through a horizontal scroll.

My problem now appears when I want to pass the ContentOffset value into the collectionView2 scrollViewDidScroll to the cursor in the collectionView 1.

I tried to initialize the collectionview 1 class in the collectionView 2 class to use the cursor directly but I do not get any results.

Can you help me understand how to solve this problem?

This is Class CollectionView 1 ( Menu )

-(instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupControl];
    }
    return self;
}

-(void)setupControl {
    self.delegate = self;
    self.dataSource = self;
    self.pagingEnabled = YES;
    self.showsHorizontalScrollIndicator = NO;
    UINib *nib = [UINib nibWithNibName:@"UDSectionNavCell" bundle: nil];
    [self registerNib:nib forCellWithReuseIdentifier:@"SCC"];
    [self layout];

    _horizontalCursorBar = [[UDSectionNavCursor alloc] init];
    _horizontalCursorBar.frame = CGRectMake(0, self.frame.size.height - 6, self.frame.size.width / 4, 3);
    _horizontalCursorBar.layer.cornerRadius = 3;
    _horizontalCursorBar.backgroundColor = [UIColor colorWithHexString:@"#C1C1C1" setAlpha:1];
    _horizontalCursorBar.alpha = 1;
    [self addSubview:_horizontalCursorBar];

    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    [self selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
}

-(void)layout {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.itemSize = CGSizeMake(self.frame.size.width / 4, self.frame.size.height);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionViewLayout = layout;
    [self reloadData];
}

-(NSArray *)dataPage {
    return [NSArray arrayWithObjects:@"ATENEO",@"STATISTICHE",@"CALENDARIO",@"NOTIFICHE", nil];
}

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

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *cellID = @"SCC";
    UDSectionNavCell *cell = (UDSectionNavCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.titlePage.text = [[self dataPage] objectAtIndex:indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:indexPath.row inSection:0];
    [self scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];

    if (indexPath == newIndexPath) {
        [self scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }

    [UIView animateWithDuration:.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseOut animations:^{
        CGFloat x = (indexPath.item) * self.frame.size.width / 4;
        _horizontalCursorBar.frame = CGRectMake(x, self.frame.size.height - 6, self.frame.size.width / 4, 3);
    } completion:nil];
}

This is the code of CollectionView 2 (page cell)

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

    UICollectionViewFlowLayout *collectionLayout = [[UICollectionViewFlowLayout alloc] init];
    collectionLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    collectionLayout.minimumLineSpacing = 0;
    self.collectionView.collectionViewLayout = collectionLayout;
    [self.collectionView  reloadData];
    self.collectionView.pagingEnabled = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
}

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

    NSArray *array = [NSArray arrayWithObjects:[UIColor redColor],[UIColor blueColor], [UIColor clearColor], [UIColor grayColor], nil];
    cell.backgroundColor = array[indexPath.item];


    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

    NSLog(@"%f",scrollView.contentOffset.x / 4);
}

I would need to pass the ContentOffset value of the scrollViewDidScroll method in the collectionView 2 class and use it in the collectionView class 1

kAiN
  • 2,559
  • 1
  • 26
  • 54
  • It sounds as if you have 2 different "collectionview 1" objects, one with the value you want and the other a new one without it. But I'm just guessing from your description. *Show actual code if you want a better answer.* – Phillip Mills Sep 21 '17 at 18:25
  • (BTW, you keep saying "class" when you probably mean "object". A confusion between those ideas might be part of your problem.) – Phillip Mills Sep 21 '17 at 18:27
  • i edited my post I would need to pass the ContentOffset value of the scrollViewDidScroll method in the collectionView 2 class and use it in the collectionView class 1 – kAiN Sep 21 '17 at 18:33
  • UICollectionView 1 has its own custom UICollectionView class UICollectionView 2 has an UICollectionViewController class UICollectionView is a class I think I will not miss this :( – kAiN Sep 21 '17 at 18:35

0 Answers0