1

I have a collectionView that is the width of the screen and I need it to switch to the next section instead to retrieve the one out of view on the right. I happen to get it where it works 1 time with this code and then it stops working, although the program works fine with no errors.

@objc func RightButtonClick(button1: UIButton) {
        let indexOfCell = button1.tag
        let visibleItems: NSArray = self.CollectionView2.indexPathsForVisibleItems as NSArray
        let currentItem: IndexPath = visibleItems.object(at: 0) as! IndexPath
        let nextItem: IndexPath = IndexPath(item: currentItem.item + indexOfCell, section: 0)
        if nextItem.row == CollectionView2.numberOfSections {
            self.CollectionView2.scrollToItem(at: nextItem, at: .left, animated: true)
        }
    }
}
Douglas
  • 65
  • 10
  • Im not sure but you can try to get the last visible item in collectionView instead get the first item. Using this code `let currentItem: IndexPath = visibleItems.lastObject as! IndexPath` – Trai Nguyen May 07 '20 at 03:07

1 Answers1

0

Your nextItem is set to section 0, so you will never get to the next section. Try this:

@objc func RightButtonClick() {
    let visibleItems: NSArray = self.collectionView.indexPathsForVisibleItems as NSArray
    guard let currentItem: IndexPath = visibleItems.object(at: 0) as? IndexPath else { return }
    print(currentItem.section)
    print(collectionView.numberOfSections)
    if collectionView.numberOfSections > currentItem.section + 1 {
        let nextSection: IndexPath = IndexPath(item: 0, section: currentItem.section + 1)
        self.collectionView.scrollToItem(at: nextSection, at: .left, animated: true)
    }
}

Note: You shouldn't need a button tag to do something like this, so that is removed.

If you only have 1 section and you're trying to go to the next item in the section, try this:

@objc func RightButtonClick() {
    let visibleItems: NSArray = self.collectionView.indexPathsForVisibleItems as NSArray
    guard let currentItem: IndexPath = visibleItems.object(at: 0) as? IndexPath else { return }
    print(currentItem.section)
    print(collectionView.numberOfSections)
    if collectionView.numberOfItems(inSection: 0) > currentItem.row + 1 {
        let nextItem: IndexPath = IndexPath(item: currentItem.row + 1, section: 0)
        self.collectionView.scrollToItem(at: nextItem, at: .centeredHorizontally, animated: true)
    }
}
elliott-io
  • 1,394
  • 6
  • 9
  • It does work for the first one but I noticed after that it jumps to the 4th section and then stops working, even if I had 10 sections. Any reason why it would be doing this? – Douglas May 07 '20 at 05:07
  • Hmm, what output are you getting for printing `currentItem.section` and `currentItem.numberOfSections` ? Also maybe try printing the `visibleItems.count`. It should only be one since you mentioned your collectionView has items that are the width of the screen. – elliott-io May 07 '20 at 05:16
  • Thanks @Douglas, thanks odd. It looks like it's skipping a section. It should go 0, 1, 2, 3, 4, etc. To confirm, is your collection view only displaying one cell at a time? – elliott-io May 07 '20 at 06:02
  • Oh, we are setting the scrollToItem to .left instead of `.centeredHorizontally`. I've adjusted my answer to fix this. Can you let me know if that corrects the section skipping? – elliott-io May 07 '20 at 06:03
  • Alright. What does `print(nextItem)` give you before running `self.collectionView.scrollToItem`? – elliott-io May 07 '20 at 06:46
  • Ok, so we are clearly skipping a row here, but still in the same section. The first number is the row, the second the section. How many items do you have per section? And can you confirm ou're using the `nextSection` code? – elliott-io May 07 '20 at 09:06
  • @Douglas, hmm, please post your full code so I can see what the issue is. – elliott-io May 07 '20 at 20:57
  • I figured out what the problem was! When Estimated Size on the collectionView is checked to Automatic, it causes the issue. So when I check None instead, it works. – Douglas May 08 '20 at 01:16
  • This helped me figure it out: https://stackoverflow.com/questions/48154984/why-does-my-uitableview-jump-when-inserting-or-removing-a-row – Douglas May 08 '20 at 01:45
  • @Douglas, great! Happy to hear you are all set now. :-) – elliott-io May 08 '20 at 18:00