Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong IndexPath on Cells via OjbC Bridging #20

Open
duraki opened this issue Mar 19, 2020 · 1 comment
Open

Wrong IndexPath on Cells via OjbC Bridging #20

duraki opened this issue Mar 19, 2020 · 1 comment

Comments

@duraki
Copy link

duraki commented Mar 19, 2020

Not sure if this is up to your library but I'll try nevertheless.

I'm using CardsCollectionViewLayout in my code via Swift Bridging for my CollectionView.

Objective-C (Not Working)

My issue is that IndexPath is returning wrong Item index. Here is the minimal code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.collectionViewEvents.collectionViewLayout = [[CardsCollectionViewLayout alloc] init];
    self.collectionViewEvents.dataSource = self;
    self.collectionViewEvents.delegate = self;
    self.collectionViewEvents.pagingEnabled = YES;
    self.collectionViewEvents.showsHorizontalScrollIndicator = NO;

    [self load];
}

// Issue visible here, the indexPath.item is not correct
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    for (UICollectionViewCell *cell in self.collectionViewEvents.visibleCells) {
        NSIndexPath *indexPath = [[self collectionViewEvents] indexPathForCell:cell];
        NSLog(@"Visible Cell IndexPath Item %ld", indexPath.item);
        return;
    }
}

- (void)load
{

    // self.eventData is declares as: @property (nonatomic) NSArray *eventData;
    self.eventData = [[NSArray alloc] initWithObjects:UIColor.blackColor, UIColor.whiteColor, UIColor.brownColor, nil];
    [[self collectionViewEvents] reloadData];
}

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

    cell.layer.cornerRadius = 7.0;
    cell.backgroundColor = UIColor.blackColor;

    return cell;
}

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

Result (notice how both 1st and 2nd cell has same IndexPath Item):

2020-03-19 14:48:05.334905+0100 App[7422:2617858] Visible Cell IndexPath Item 2 # => 3rd cell
2020-03-19 14:48:05.741805+0100 App[7422:2617858] Visible Cell IndexPath Item 1 # => 2nd cell
2020-03-19 14:48:06.184932+0100 App[7422:2617858] Visible Cell IndexPath Item 1 # => 1st cell

Swift (Working)

I tried your Example from this code Repository, which has declared colors statically:

  var colors: [UIColor]  = [
    UIColor(red: 237, green: 37, blue: 78),
    UIColor(red: 249, green: 220, blue: 92),
    UIColor(red: 194, green: 234, blue: 189),
    UIColor(red: 1, green: 25, blue: 54),
    UIColor(red: 255, green: 184, blue: 209)
  ]
...

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCellReuseIdentifier", for: indexPath)

        cell.layer.cornerRadius = 7.0
        cell.backgroundColor = .black

        return cell
    }

And after implementing this code below, it returns proper IndexPath.

    // Issue NOT visible here, the indexPath.item IS correct
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

        for cell in collectionView.visibleCells {
            let indexPath = collectionView.indexPath(for: cell)
            print(indexPath?.item)
            return
        }
    }

Result:

2020-03-19 14:48:05.334905+0100 App[7422:2617858] Visible Cell IndexPath Item 2 # => 3rd cell
2020-03-19 14:48:05.741805+0100 App[7422:2617858] Visible Cell IndexPath Item 1 # => 2nd cell
2020-03-19 14:48:06.184932+0100 App[7422:2617858] Visible Cell IndexPath Item 0 # => 1st cell

What am I doing wrong in my Objective-C code?

Things I've tried

// 1st cell has index 1, instead of 0
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    for (UICollectionViewCell *cell in [[self collectionViewEvents] visibleCells]) {
        NSIndexPath *indexPath = [[self collectionViewEvents] indexPathForCell:cell];
        NSLog(@"Visible Cell IndexPath Item %ld", indexPath.item);
        return;
    }
// 1st cell has index 1, instead of 0
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSArray *visible = [self.collectionViewEvents indexPathsForVisibleItems];
    NSIndexPath *indexPath = [visible firstObject];
    NSLog(@"Visible Cell IndexPath Item %ld", indexPath.item);
    return;
// Calling it in main thread, same result
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    dispatch_async(dispatch_get_main_queue(), ^{
        for (UICollectionViewCell *cell in [[self collectionViewEvents] visibleCells]) {
            NSIndexPath *indexPath = [[self collectionViewEvents] indexPathForCell:cell];
            NSLog(@"Visible Cell IndexPath Item %ld", indexPath.item);
            return;
        }
    });
@filletofish
Copy link
Owner

Hi,

Thank you for reporting this issue.

Unfortunately, I currently do not have a bandwidth to look into this issue. In case you manage to fix that I would be happy to accept PR.

Good luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants