UICollectionView current visible cell index











up vote
86
down vote

favorite
29












I am using UICollectionView first time in my iPad application.
I have set UICollectionView such that its size and cell size is same, means only once cell is displayed at a time.



Problem:
Now when user scroll UICollectionView I need to know which cell is visible I have to update other UI elements on change. I didn't find any delegate method for this. How can I achieve this?



Code:



[self.mainImageCollection setTag:MAIN_IMAGE_COLLECTION_VIEW];
[self.mainImageCollection registerClass:[InspirationMainImageCollectionCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[self.mainImageFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.mainImageFlowLayout setMinimumInteritemSpacing:0.0f];
[self.mainImageFlowLayout setMinimumLineSpacing:0.0f];
self.mainImageFlowLayout.minimumLineSpacing = 0;
[self.mainImageCollection setPagingEnabled:YES];
[self.mainImageCollection setShowsHorizontalScrollIndicator:NO];
[self.mainImageCollection setCollectionViewLayout:self.mainImageFlowLayout];


What I have tried:



As UICollectionView Conforms to UIScrollView, I got when user scroll ends with UIScrollViewDelegate method



-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView


But inside above function how can I get current visible cell index of UICollectionView ???










share|improve this question






















  • self.collectionViewFloors.indexPathsForVisibleItems
    – Aznix
    Oct 29 '16 at 12:07















up vote
86
down vote

favorite
29












I am using UICollectionView first time in my iPad application.
I have set UICollectionView such that its size and cell size is same, means only once cell is displayed at a time.



Problem:
Now when user scroll UICollectionView I need to know which cell is visible I have to update other UI elements on change. I didn't find any delegate method for this. How can I achieve this?



Code:



[self.mainImageCollection setTag:MAIN_IMAGE_COLLECTION_VIEW];
[self.mainImageCollection registerClass:[InspirationMainImageCollectionCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[self.mainImageFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.mainImageFlowLayout setMinimumInteritemSpacing:0.0f];
[self.mainImageFlowLayout setMinimumLineSpacing:0.0f];
self.mainImageFlowLayout.minimumLineSpacing = 0;
[self.mainImageCollection setPagingEnabled:YES];
[self.mainImageCollection setShowsHorizontalScrollIndicator:NO];
[self.mainImageCollection setCollectionViewLayout:self.mainImageFlowLayout];


What I have tried:



As UICollectionView Conforms to UIScrollView, I got when user scroll ends with UIScrollViewDelegate method



-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView


But inside above function how can I get current visible cell index of UICollectionView ???










share|improve this question






















  • self.collectionViewFloors.indexPathsForVisibleItems
    – Aznix
    Oct 29 '16 at 12:07













up vote
86
down vote

favorite
29









up vote
86
down vote

favorite
29






29





I am using UICollectionView first time in my iPad application.
I have set UICollectionView such that its size and cell size is same, means only once cell is displayed at a time.



Problem:
Now when user scroll UICollectionView I need to know which cell is visible I have to update other UI elements on change. I didn't find any delegate method for this. How can I achieve this?



Code:



[self.mainImageCollection setTag:MAIN_IMAGE_COLLECTION_VIEW];
[self.mainImageCollection registerClass:[InspirationMainImageCollectionCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[self.mainImageFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.mainImageFlowLayout setMinimumInteritemSpacing:0.0f];
[self.mainImageFlowLayout setMinimumLineSpacing:0.0f];
self.mainImageFlowLayout.minimumLineSpacing = 0;
[self.mainImageCollection setPagingEnabled:YES];
[self.mainImageCollection setShowsHorizontalScrollIndicator:NO];
[self.mainImageCollection setCollectionViewLayout:self.mainImageFlowLayout];


What I have tried:



As UICollectionView Conforms to UIScrollView, I got when user scroll ends with UIScrollViewDelegate method



-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView


But inside above function how can I get current visible cell index of UICollectionView ???










share|improve this question













I am using UICollectionView first time in my iPad application.
I have set UICollectionView such that its size and cell size is same, means only once cell is displayed at a time.



Problem:
Now when user scroll UICollectionView I need to know which cell is visible I have to update other UI elements on change. I didn't find any delegate method for this. How can I achieve this?



Code:



[self.mainImageCollection setTag:MAIN_IMAGE_COLLECTION_VIEW];
[self.mainImageCollection registerClass:[InspirationMainImageCollectionCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[self.mainImageFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.mainImageFlowLayout setMinimumInteritemSpacing:0.0f];
[self.mainImageFlowLayout setMinimumLineSpacing:0.0f];
self.mainImageFlowLayout.minimumLineSpacing = 0;
[self.mainImageCollection setPagingEnabled:YES];
[self.mainImageCollection setShowsHorizontalScrollIndicator:NO];
[self.mainImageCollection setCollectionViewLayout:self.mainImageFlowLayout];


What I have tried:



As UICollectionView Conforms to UIScrollView, I got when user scroll ends with UIScrollViewDelegate method



-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView


But inside above function how can I get current visible cell index of UICollectionView ???







ios objective-c ipad uicollectionview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 6 '13 at 4:30









Irfan DANISH

4,81283155




4,81283155












  • self.collectionViewFloors.indexPathsForVisibleItems
    – Aznix
    Oct 29 '16 at 12:07


















  • self.collectionViewFloors.indexPathsForVisibleItems
    – Aznix
    Oct 29 '16 at 12:07
















self.collectionViewFloors.indexPathsForVisibleItems
– Aznix
Oct 29 '16 at 12:07




self.collectionViewFloors.indexPathsForVisibleItems
– Aznix
Oct 29 '16 at 12:07












15 Answers
15






active

oldest

votes

















up vote
111
down vote



accepted










The method [collectionView visibleCells] give you all visibleCells array you want. Use it when you want to get



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
for (UICollectionViewCell *cell in [self.mainImageCollection visibleCells]) {
NSIndexPath *indexPath = [self.mainImageCollection indexPathForCell:cell];
NSLog(@"%@",indexPath);
}
}





share|improve this answer





















  • Can you please elaborate on where self.mainImageCollection comes from? Many thanx in advance for your further detail.
    – Benjamin McFerren
    Jun 1 '14 at 21:57










  • @BenjaminMcFerren it's the collectionview you used.
    – LE SANG
    Jun 2 '14 at 0:37






  • 9




    The scrollViewDidScroll: delegate method provides scroll view updates whenever any scroll finishes (and is probably a better choice here). As opposed to the scrollViewDidEndDecelerating: delegate method which is only called when the scroll view "grinds to a halt [from a big scroll]" (see UIScrollView header).
    – Samuel Spencer
    Sep 16 '14 at 17:52








  • 1




    IIRC, ..DidScroll gets called many times, even during a short scroll. May or may not be a good thing, depending on what one wants to do.
    – ToolmakerSteve
    Jun 28 '16 at 12:39






  • 4




    Since iOS 10 it's now also possible to use indexPathsForVisibleItems directly :)
    – Ben
    Nov 7 '16 at 14:03


















up vote
132
down vote













indexPathsForVisibleItems might work for most situations, but sometimes it returns an array with more than one index path and it can be tricky figuring out the one you want. In those situations, you can do something like this:



CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];


This works especially well when each item in your collection view takes up the whole screen.



Swift version



let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
let visibleIndexPath = collectionView.indexPathForItem(at: visiblePoint)





share|improve this answer



















  • 2




    This is great - thank you!
    – mmackh
    Sep 15 '14 at 11:55






  • 8




    I can agree, sometimes indexPathsForVisibleItems returns more cells, even if we think there shouldn't be such case. Your solution works great.
    – MP23
    Sep 17 '14 at 15:07






  • 4




    this is the most accurate code!
    – Hashem Aboonajmi
    Sep 23 '14 at 12:52






  • 2




    I was totally on this kind of situation, just one fix that worked for me (but my case was with tableview), I needed to change the CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect)); for CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMinY(visibleRect));
    – JRafaelM
    May 12 '15 at 16:22






  • 1




    Excellent, but if the cells have a space between them then visibleIndexPath sometimes will be nil, So if (visibleIndexPath) == nil { let cells = collectionView.visibleCells() let visibleIndexPath = collectionView.indexPathForCell(cells[0] as! UICollectionViewCell)! as NSIndexPath }
    – Husam
    Jun 19 '15 at 17:19




















up vote
62
down vote













Working Answers Combined In Swift 2.2 :



 func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

var visibleRect = CGRect()

visibleRect.origin = self.collectionView.contentOffset
visibleRect.size = self.collectionView.bounds.size

let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))

let visibleIndexPath: NSIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)

guard let indexPath = visibleIndexPath else { return }
print(indexPath)

}


Swift 3 & Swift 4:



func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var visibleRect = CGRect()

visibleRect.origin = collectionView.contentOffset
visibleRect.size = collectionView.bounds.size

let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)

guard let indexPath = collectionView.indexPathForItem(at: visiblePoint) else { return }

print(indexPath)
}





share|improve this answer






























    up vote
    16
    down vote













    For completeness sake, this is the method that ended up working for me. It was a combination of @Anthony & @iAn's methods.



    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
    CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
    NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];
    NSLog(@"%@",visibleIndexPath);
    }





    share|improve this answer






























      up vote
      8
      down vote













      Just want to add for others :
      for some reason, I didnt not get the cell that was visible to the user when I was scrolling to previous cell in collectionView with pagingEnabled.



      So I insert the code inside dispatch_async to give it some "air"
      and this works for me.



      -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
      {
      dispatch_async(dispatch_get_main_queue(), ^{
      UICollectionViewCell * visibleCell= [[self.collectionView visibleCells] objectAtIndex:0];


      [visibleCell doSomthing];
      });
      }





      share|improve this answer























      • This really helped! I didn't realized I can use dispatch_async block to make it absolutely flawless! This is the best answer!
        – kalafun
        Oct 20 '15 at 15:45






      • 1




        For Swift 4: DispatchQueue.main.async { when call initiates from something like func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        – Jonny
        Dec 26 '17 at 10:33












      • Some clarification: this helped me too, and I had the same/similar problem, when scrolling back to uitableviewcell containing a uicollectionview. The refresh call initiated from willDisplay cell as mentioned above. I think the problem, somewhere in UIKit, is really the same in my case as this answer.
        – Jonny
        Dec 26 '17 at 10:39


















      up vote
      8
      down vote













      It will probably be best to use UICollectionViewDelegate methods: (Swift 3)



      // Called before the cell is displayed    
      func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
      print(indexPath.row)
      }

      // Called when the cell is displayed
      func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
      print(indexPath.row)
      }





      share|improve this answer

















      • 2




        Regarding didEndDisplaying, from the docs: Tells the delegate that the specified cell was removed from the collection view. Use this method to detect when a cell is removed from a collection view, as opposed to monitoring the view itself to see when it disappears. So I do not think didEndDisplaying is called when the cell is displayed.
        – Jonny
        Dec 26 '17 at 10:49


















      up vote
      7
      down vote














      For Swift 3.0




      func scrollViewDidScroll(_ scrollView: UIScrollView) {
      let visibleRect = CGRect(origin: colView.contentOffset, size: colView.bounds.size)
      let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
      let indexPath = colView.indexPathForItem(at: visiblePoint)
      }





      share|improve this answer






























        up vote
        5
        down vote













        Swift 3.0



        Simplest solution which will give you indexPath for visible cells..



        yourCollectionView.indexPathsForVisibleItems 


        will return the array of indexpath.



        Just take the first object from array like this.



        yourCollectionView.indexPathsForVisibleItems.first


        I guess it should work fine with Objective - C as well.






        share|improve this answer






























          up vote
          2
          down vote













          You can use scrollViewDidEndDecelerating: for this



          //@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

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

          for (UICollectionViewCell *cell in [self.collectionView visibleCells]) {
          NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
          NSUInteger lastIndex = [indexPath indexAtPosition:[indexPath length] - 1];
          NSLog(@"visible cell value %d",lastIndex);
          }

          }





          share|improve this answer






























            up vote
            2
            down vote













            UICollectionView current visible cell index: Swift 3



            var visibleCurrentCellIndexPath: IndexPath? {
            for cell in self.collectionView.visibleCells {
            let indexPath = self.collectionView.indexPath(for: cell)
            return indexPath
            }

            return nil
            }





            share|improve this answer























            • Best answer. Clean and a few lines.
              – garanda
              Dec 1 '16 at 8:45


















            up vote
            1
            down vote













            converting @Anthony's answer to Swift 3.0 worked perfectly for me:



            func scrollViewDidScroll(_ scrollView: UIScrollView) {

            var visibleRect = CGRect()
            visibleRect.origin = yourCollectionView.contentOffset
            visibleRect.size = yourCollectionView.bounds.size
            let visiblePoint = CGPoint(x: CGFloat(visibleRect.midX), y: CGFloat(visibleRect.midY))
            let visibleIndexPath: IndexPath? = yourCollectionView.indexPathForItem(at: visiblePoint)
            print("Visible cell's index is : (visibleIndexPath?.row)!")
            }





            share|improve this answer




























              up vote
              0
              down vote













              This is old question but in my case...



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

              _m_offsetIdx = [m_cv indexPathForCell:m_cv.visibleCells.firstObject].row;
              }

              - (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
              _m_offsetIdx = [m_cv indexPathForCell:m_cv.visibleCells.lastObject].row;
              }





              share|improve this answer




























                up vote
                0
                down vote













                Also check this snippet



                let isCellVisible = collectionView.visibleCells.map { collectionView.indexPath(for: $0) }.contains(inspectingIndexPath)





                share|improve this answer




























                  up vote
                  0
                  down vote













                  Swift 3 & Swift 4:



                  func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
                  var visibleRect = CGRect()

                  visibleRect.origin = collectionView.contentOffset
                  visibleRect.size = collectionView.bounds.size

                  let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)

                  guard let indexPath = collectionView.indexPathForItem(at: visiblePoint) else { return }

                  print(indexPath[1])
                  }


                  If you want to show actual number than you can add +1






                  share|improve this answer




























                    up vote
                    -1
                    down vote













                    try this, it works. (in the example below i have 3 cells for example.)



                        func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
                    let visibleRect = CGRect(origin: self.collectionView.contentOffset, size: self.collectionView.bounds.size)
                    let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))
                    let visibleIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)
                    if let v = visibleIndexPath {
                    switch v.item {
                    case 0: setImageDescription()
                    break
                    case 1: setImageConditions()
                    break
                    case 2: setImageResults()
                    break
                    default: break
                    }
                    }





                    share|improve this answer





















                      Your Answer






                      StackExchange.ifUsing("editor", function () {
                      StackExchange.using("externalEditor", function () {
                      StackExchange.using("snippets", function () {
                      StackExchange.snippets.init();
                      });
                      });
                      }, "code-snippets");

                      StackExchange.ready(function() {
                      var channelOptions = {
                      tags: "".split(" "),
                      id: "1"
                      };
                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                      StackExchange.using("externalEditor", function() {
                      // Have to fire editor after snippets, if snippets enabled
                      if (StackExchange.settings.snippets.snippetsEnabled) {
                      StackExchange.using("snippets", function() {
                      createEditor();
                      });
                      }
                      else {
                      createEditor();
                      }
                      });

                      function createEditor() {
                      StackExchange.prepareEditor({
                      heartbeatType: 'answer',
                      convertImagesToLinks: true,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: 10,
                      bindNavPrevention: true,
                      postfix: "",
                      imageUploader: {
                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                      allowUrls: true
                      },
                      onDemand: true,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      });


                      }
                      });














                      draft saved

                      draft discarded


















                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18649920%2fuicollectionview-current-visible-cell-index%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown

























                      15 Answers
                      15






                      active

                      oldest

                      votes








                      15 Answers
                      15






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes








                      up vote
                      111
                      down vote



                      accepted










                      The method [collectionView visibleCells] give you all visibleCells array you want. Use it when you want to get



                      - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
                      for (UICollectionViewCell *cell in [self.mainImageCollection visibleCells]) {
                      NSIndexPath *indexPath = [self.mainImageCollection indexPathForCell:cell];
                      NSLog(@"%@",indexPath);
                      }
                      }





                      share|improve this answer





















                      • Can you please elaborate on where self.mainImageCollection comes from? Many thanx in advance for your further detail.
                        – Benjamin McFerren
                        Jun 1 '14 at 21:57










                      • @BenjaminMcFerren it's the collectionview you used.
                        – LE SANG
                        Jun 2 '14 at 0:37






                      • 9




                        The scrollViewDidScroll: delegate method provides scroll view updates whenever any scroll finishes (and is probably a better choice here). As opposed to the scrollViewDidEndDecelerating: delegate method which is only called when the scroll view "grinds to a halt [from a big scroll]" (see UIScrollView header).
                        – Samuel Spencer
                        Sep 16 '14 at 17:52








                      • 1




                        IIRC, ..DidScroll gets called many times, even during a short scroll. May or may not be a good thing, depending on what one wants to do.
                        – ToolmakerSteve
                        Jun 28 '16 at 12:39






                      • 4




                        Since iOS 10 it's now also possible to use indexPathsForVisibleItems directly :)
                        – Ben
                        Nov 7 '16 at 14:03















                      up vote
                      111
                      down vote



                      accepted










                      The method [collectionView visibleCells] give you all visibleCells array you want. Use it when you want to get



                      - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
                      for (UICollectionViewCell *cell in [self.mainImageCollection visibleCells]) {
                      NSIndexPath *indexPath = [self.mainImageCollection indexPathForCell:cell];
                      NSLog(@"%@",indexPath);
                      }
                      }





                      share|improve this answer





















                      • Can you please elaborate on where self.mainImageCollection comes from? Many thanx in advance for your further detail.
                        – Benjamin McFerren
                        Jun 1 '14 at 21:57










                      • @BenjaminMcFerren it's the collectionview you used.
                        – LE SANG
                        Jun 2 '14 at 0:37






                      • 9




                        The scrollViewDidScroll: delegate method provides scroll view updates whenever any scroll finishes (and is probably a better choice here). As opposed to the scrollViewDidEndDecelerating: delegate method which is only called when the scroll view "grinds to a halt [from a big scroll]" (see UIScrollView header).
                        – Samuel Spencer
                        Sep 16 '14 at 17:52








                      • 1




                        IIRC, ..DidScroll gets called many times, even during a short scroll. May or may not be a good thing, depending on what one wants to do.
                        – ToolmakerSteve
                        Jun 28 '16 at 12:39






                      • 4




                        Since iOS 10 it's now also possible to use indexPathsForVisibleItems directly :)
                        – Ben
                        Nov 7 '16 at 14:03













                      up vote
                      111
                      down vote



                      accepted







                      up vote
                      111
                      down vote



                      accepted






                      The method [collectionView visibleCells] give you all visibleCells array you want. Use it when you want to get



                      - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
                      for (UICollectionViewCell *cell in [self.mainImageCollection visibleCells]) {
                      NSIndexPath *indexPath = [self.mainImageCollection indexPathForCell:cell];
                      NSLog(@"%@",indexPath);
                      }
                      }





                      share|improve this answer












                      The method [collectionView visibleCells] give you all visibleCells array you want. Use it when you want to get



                      - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
                      for (UICollectionViewCell *cell in [self.mainImageCollection visibleCells]) {
                      NSIndexPath *indexPath = [self.mainImageCollection indexPathForCell:cell];
                      NSLog(@"%@",indexPath);
                      }
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Sep 6 '13 at 4:59









                      LE SANG

                      8,46475174




                      8,46475174












                      • Can you please elaborate on where self.mainImageCollection comes from? Many thanx in advance for your further detail.
                        – Benjamin McFerren
                        Jun 1 '14 at 21:57










                      • @BenjaminMcFerren it's the collectionview you used.
                        – LE SANG
                        Jun 2 '14 at 0:37






                      • 9




                        The scrollViewDidScroll: delegate method provides scroll view updates whenever any scroll finishes (and is probably a better choice here). As opposed to the scrollViewDidEndDecelerating: delegate method which is only called when the scroll view "grinds to a halt [from a big scroll]" (see UIScrollView header).
                        – Samuel Spencer
                        Sep 16 '14 at 17:52








                      • 1




                        IIRC, ..DidScroll gets called many times, even during a short scroll. May or may not be a good thing, depending on what one wants to do.
                        – ToolmakerSteve
                        Jun 28 '16 at 12:39






                      • 4




                        Since iOS 10 it's now also possible to use indexPathsForVisibleItems directly :)
                        – Ben
                        Nov 7 '16 at 14:03


















                      • Can you please elaborate on where self.mainImageCollection comes from? Many thanx in advance for your further detail.
                        – Benjamin McFerren
                        Jun 1 '14 at 21:57










                      • @BenjaminMcFerren it's the collectionview you used.
                        – LE SANG
                        Jun 2 '14 at 0:37






                      • 9




                        The scrollViewDidScroll: delegate method provides scroll view updates whenever any scroll finishes (and is probably a better choice here). As opposed to the scrollViewDidEndDecelerating: delegate method which is only called when the scroll view "grinds to a halt [from a big scroll]" (see UIScrollView header).
                        – Samuel Spencer
                        Sep 16 '14 at 17:52








                      • 1




                        IIRC, ..DidScroll gets called many times, even during a short scroll. May or may not be a good thing, depending on what one wants to do.
                        – ToolmakerSteve
                        Jun 28 '16 at 12:39






                      • 4




                        Since iOS 10 it's now also possible to use indexPathsForVisibleItems directly :)
                        – Ben
                        Nov 7 '16 at 14:03
















                      Can you please elaborate on where self.mainImageCollection comes from? Many thanx in advance for your further detail.
                      – Benjamin McFerren
                      Jun 1 '14 at 21:57




                      Can you please elaborate on where self.mainImageCollection comes from? Many thanx in advance for your further detail.
                      – Benjamin McFerren
                      Jun 1 '14 at 21:57












                      @BenjaminMcFerren it's the collectionview you used.
                      – LE SANG
                      Jun 2 '14 at 0:37




                      @BenjaminMcFerren it's the collectionview you used.
                      – LE SANG
                      Jun 2 '14 at 0:37




                      9




                      9




                      The scrollViewDidScroll: delegate method provides scroll view updates whenever any scroll finishes (and is probably a better choice here). As opposed to the scrollViewDidEndDecelerating: delegate method which is only called when the scroll view "grinds to a halt [from a big scroll]" (see UIScrollView header).
                      – Samuel Spencer
                      Sep 16 '14 at 17:52






                      The scrollViewDidScroll: delegate method provides scroll view updates whenever any scroll finishes (and is probably a better choice here). As opposed to the scrollViewDidEndDecelerating: delegate method which is only called when the scroll view "grinds to a halt [from a big scroll]" (see UIScrollView header).
                      – Samuel Spencer
                      Sep 16 '14 at 17:52






                      1




                      1




                      IIRC, ..DidScroll gets called many times, even during a short scroll. May or may not be a good thing, depending on what one wants to do.
                      – ToolmakerSteve
                      Jun 28 '16 at 12:39




                      IIRC, ..DidScroll gets called many times, even during a short scroll. May or may not be a good thing, depending on what one wants to do.
                      – ToolmakerSteve
                      Jun 28 '16 at 12:39




                      4




                      4




                      Since iOS 10 it's now also possible to use indexPathsForVisibleItems directly :)
                      – Ben
                      Nov 7 '16 at 14:03




                      Since iOS 10 it's now also possible to use indexPathsForVisibleItems directly :)
                      – Ben
                      Nov 7 '16 at 14:03












                      up vote
                      132
                      down vote













                      indexPathsForVisibleItems might work for most situations, but sometimes it returns an array with more than one index path and it can be tricky figuring out the one you want. In those situations, you can do something like this:



                      CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
                      CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
                      NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];


                      This works especially well when each item in your collection view takes up the whole screen.



                      Swift version



                      let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
                      let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
                      let visibleIndexPath = collectionView.indexPathForItem(at: visiblePoint)





                      share|improve this answer



















                      • 2




                        This is great - thank you!
                        – mmackh
                        Sep 15 '14 at 11:55






                      • 8




                        I can agree, sometimes indexPathsForVisibleItems returns more cells, even if we think there shouldn't be such case. Your solution works great.
                        – MP23
                        Sep 17 '14 at 15:07






                      • 4




                        this is the most accurate code!
                        – Hashem Aboonajmi
                        Sep 23 '14 at 12:52






                      • 2




                        I was totally on this kind of situation, just one fix that worked for me (but my case was with tableview), I needed to change the CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect)); for CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMinY(visibleRect));
                        – JRafaelM
                        May 12 '15 at 16:22






                      • 1




                        Excellent, but if the cells have a space between them then visibleIndexPath sometimes will be nil, So if (visibleIndexPath) == nil { let cells = collectionView.visibleCells() let visibleIndexPath = collectionView.indexPathForCell(cells[0] as! UICollectionViewCell)! as NSIndexPath }
                        – Husam
                        Jun 19 '15 at 17:19

















                      up vote
                      132
                      down vote













                      indexPathsForVisibleItems might work for most situations, but sometimes it returns an array with more than one index path and it can be tricky figuring out the one you want. In those situations, you can do something like this:



                      CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
                      CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
                      NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];


                      This works especially well when each item in your collection view takes up the whole screen.



                      Swift version



                      let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
                      let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
                      let visibleIndexPath = collectionView.indexPathForItem(at: visiblePoint)





                      share|improve this answer



















                      • 2




                        This is great - thank you!
                        – mmackh
                        Sep 15 '14 at 11:55






                      • 8




                        I can agree, sometimes indexPathsForVisibleItems returns more cells, even if we think there shouldn't be such case. Your solution works great.
                        – MP23
                        Sep 17 '14 at 15:07






                      • 4




                        this is the most accurate code!
                        – Hashem Aboonajmi
                        Sep 23 '14 at 12:52






                      • 2




                        I was totally on this kind of situation, just one fix that worked for me (but my case was with tableview), I needed to change the CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect)); for CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMinY(visibleRect));
                        – JRafaelM
                        May 12 '15 at 16:22






                      • 1




                        Excellent, but if the cells have a space between them then visibleIndexPath sometimes will be nil, So if (visibleIndexPath) == nil { let cells = collectionView.visibleCells() let visibleIndexPath = collectionView.indexPathForCell(cells[0] as! UICollectionViewCell)! as NSIndexPath }
                        – Husam
                        Jun 19 '15 at 17:19















                      up vote
                      132
                      down vote










                      up vote
                      132
                      down vote









                      indexPathsForVisibleItems might work for most situations, but sometimes it returns an array with more than one index path and it can be tricky figuring out the one you want. In those situations, you can do something like this:



                      CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
                      CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
                      NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];


                      This works especially well when each item in your collection view takes up the whole screen.



                      Swift version



                      let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
                      let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
                      let visibleIndexPath = collectionView.indexPathForItem(at: visiblePoint)





                      share|improve this answer














                      indexPathsForVisibleItems might work for most situations, but sometimes it returns an array with more than one index path and it can be tricky figuring out the one you want. In those situations, you can do something like this:



                      CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
                      CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
                      NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];


                      This works especially well when each item in your collection view takes up the whole screen.



                      Swift version



                      let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
                      let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
                      let visibleIndexPath = collectionView.indexPathForItem(at: visiblePoint)






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jun 6 at 11:05









                      Arthur

                      6610




                      6610










                      answered Jun 24 '14 at 21:28









                      Anthony

                      4,41022542




                      4,41022542








                      • 2




                        This is great - thank you!
                        – mmackh
                        Sep 15 '14 at 11:55






                      • 8




                        I can agree, sometimes indexPathsForVisibleItems returns more cells, even if we think there shouldn't be such case. Your solution works great.
                        – MP23
                        Sep 17 '14 at 15:07






                      • 4




                        this is the most accurate code!
                        – Hashem Aboonajmi
                        Sep 23 '14 at 12:52






                      • 2




                        I was totally on this kind of situation, just one fix that worked for me (but my case was with tableview), I needed to change the CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect)); for CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMinY(visibleRect));
                        – JRafaelM
                        May 12 '15 at 16:22






                      • 1




                        Excellent, but if the cells have a space between them then visibleIndexPath sometimes will be nil, So if (visibleIndexPath) == nil { let cells = collectionView.visibleCells() let visibleIndexPath = collectionView.indexPathForCell(cells[0] as! UICollectionViewCell)! as NSIndexPath }
                        – Husam
                        Jun 19 '15 at 17:19
















                      • 2




                        This is great - thank you!
                        – mmackh
                        Sep 15 '14 at 11:55






                      • 8




                        I can agree, sometimes indexPathsForVisibleItems returns more cells, even if we think there shouldn't be such case. Your solution works great.
                        – MP23
                        Sep 17 '14 at 15:07






                      • 4




                        this is the most accurate code!
                        – Hashem Aboonajmi
                        Sep 23 '14 at 12:52






                      • 2




                        I was totally on this kind of situation, just one fix that worked for me (but my case was with tableview), I needed to change the CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect)); for CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMinY(visibleRect));
                        – JRafaelM
                        May 12 '15 at 16:22






                      • 1




                        Excellent, but if the cells have a space between them then visibleIndexPath sometimes will be nil, So if (visibleIndexPath) == nil { let cells = collectionView.visibleCells() let visibleIndexPath = collectionView.indexPathForCell(cells[0] as! UICollectionViewCell)! as NSIndexPath }
                        – Husam
                        Jun 19 '15 at 17:19










                      2




                      2




                      This is great - thank you!
                      – mmackh
                      Sep 15 '14 at 11:55




                      This is great - thank you!
                      – mmackh
                      Sep 15 '14 at 11:55




                      8




                      8




                      I can agree, sometimes indexPathsForVisibleItems returns more cells, even if we think there shouldn't be such case. Your solution works great.
                      – MP23
                      Sep 17 '14 at 15:07




                      I can agree, sometimes indexPathsForVisibleItems returns more cells, even if we think there shouldn't be such case. Your solution works great.
                      – MP23
                      Sep 17 '14 at 15:07




                      4




                      4




                      this is the most accurate code!
                      – Hashem Aboonajmi
                      Sep 23 '14 at 12:52




                      this is the most accurate code!
                      – Hashem Aboonajmi
                      Sep 23 '14 at 12:52




                      2




                      2




                      I was totally on this kind of situation, just one fix that worked for me (but my case was with tableview), I needed to change the CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect)); for CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMinY(visibleRect));
                      – JRafaelM
                      May 12 '15 at 16:22




                      I was totally on this kind of situation, just one fix that worked for me (but my case was with tableview), I needed to change the CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect)); for CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMinY(visibleRect));
                      – JRafaelM
                      May 12 '15 at 16:22




                      1




                      1




                      Excellent, but if the cells have a space between them then visibleIndexPath sometimes will be nil, So if (visibleIndexPath) == nil { let cells = collectionView.visibleCells() let visibleIndexPath = collectionView.indexPathForCell(cells[0] as! UICollectionViewCell)! as NSIndexPath }
                      – Husam
                      Jun 19 '15 at 17:19






                      Excellent, but if the cells have a space between them then visibleIndexPath sometimes will be nil, So if (visibleIndexPath) == nil { let cells = collectionView.visibleCells() let visibleIndexPath = collectionView.indexPathForCell(cells[0] as! UICollectionViewCell)! as NSIndexPath }
                      – Husam
                      Jun 19 '15 at 17:19












                      up vote
                      62
                      down vote













                      Working Answers Combined In Swift 2.2 :



                       func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

                      var visibleRect = CGRect()

                      visibleRect.origin = self.collectionView.contentOffset
                      visibleRect.size = self.collectionView.bounds.size

                      let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))

                      let visibleIndexPath: NSIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)

                      guard let indexPath = visibleIndexPath else { return }
                      print(indexPath)

                      }


                      Swift 3 & Swift 4:



                      func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
                      var visibleRect = CGRect()

                      visibleRect.origin = collectionView.contentOffset
                      visibleRect.size = collectionView.bounds.size

                      let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)

                      guard let indexPath = collectionView.indexPathForItem(at: visiblePoint) else { return }

                      print(indexPath)
                      }





                      share|improve this answer



























                        up vote
                        62
                        down vote













                        Working Answers Combined In Swift 2.2 :



                         func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

                        var visibleRect = CGRect()

                        visibleRect.origin = self.collectionView.contentOffset
                        visibleRect.size = self.collectionView.bounds.size

                        let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))

                        let visibleIndexPath: NSIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)

                        guard let indexPath = visibleIndexPath else { return }
                        print(indexPath)

                        }


                        Swift 3 & Swift 4:



                        func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
                        var visibleRect = CGRect()

                        visibleRect.origin = collectionView.contentOffset
                        visibleRect.size = collectionView.bounds.size

                        let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)

                        guard let indexPath = collectionView.indexPathForItem(at: visiblePoint) else { return }

                        print(indexPath)
                        }





                        share|improve this answer

























                          up vote
                          62
                          down vote










                          up vote
                          62
                          down vote









                          Working Answers Combined In Swift 2.2 :



                           func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

                          var visibleRect = CGRect()

                          visibleRect.origin = self.collectionView.contentOffset
                          visibleRect.size = self.collectionView.bounds.size

                          let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))

                          let visibleIndexPath: NSIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)

                          guard let indexPath = visibleIndexPath else { return }
                          print(indexPath)

                          }


                          Swift 3 & Swift 4:



                          func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
                          var visibleRect = CGRect()

                          visibleRect.origin = collectionView.contentOffset
                          visibleRect.size = collectionView.bounds.size

                          let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)

                          guard let indexPath = collectionView.indexPathForItem(at: visiblePoint) else { return }

                          print(indexPath)
                          }





                          share|improve this answer














                          Working Answers Combined In Swift 2.2 :



                           func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

                          var visibleRect = CGRect()

                          visibleRect.origin = self.collectionView.contentOffset
                          visibleRect.size = self.collectionView.bounds.size

                          let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))

                          let visibleIndexPath: NSIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)

                          guard let indexPath = visibleIndexPath else { return }
                          print(indexPath)

                          }


                          Swift 3 & Swift 4:



                          func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
                          var visibleRect = CGRect()

                          visibleRect.origin = collectionView.contentOffset
                          visibleRect.size = collectionView.bounds.size

                          let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)

                          guard let indexPath = collectionView.indexPathForItem(at: visiblePoint) else { return }

                          print(indexPath)
                          }






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 22 at 19:20









                          Nik Kov

                          2,9762450




                          2,9762450










                          answered Apr 11 '16 at 12:44









                          Sam Bing

                          1,214820




                          1,214820






















                              up vote
                              16
                              down vote













                              For completeness sake, this is the method that ended up working for me. It was a combination of @Anthony & @iAn's methods.



                              - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
                              CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
                              CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
                              NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];
                              NSLog(@"%@",visibleIndexPath);
                              }





                              share|improve this answer



























                                up vote
                                16
                                down vote













                                For completeness sake, this is the method that ended up working for me. It was a combination of @Anthony & @iAn's methods.



                                - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
                                CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
                                CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
                                NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];
                                NSLog(@"%@",visibleIndexPath);
                                }





                                share|improve this answer

























                                  up vote
                                  16
                                  down vote










                                  up vote
                                  16
                                  down vote









                                  For completeness sake, this is the method that ended up working for me. It was a combination of @Anthony & @iAn's methods.



                                  - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
                                  CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
                                  CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
                                  NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];
                                  NSLog(@"%@",visibleIndexPath);
                                  }





                                  share|improve this answer














                                  For completeness sake, this is the method that ended up working for me. It was a combination of @Anthony & @iAn's methods.



                                  - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
                                  CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
                                  CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
                                  NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];
                                  NSLog(@"%@",visibleIndexPath);
                                  }






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Jun 12 '17 at 11:56









                                  Meet Doshi

                                  2,65282766




                                  2,65282766










                                  answered Sep 18 '14 at 13:06









                                  Vincil Bishop

                                  1,1411119




                                  1,1411119






















                                      up vote
                                      8
                                      down vote













                                      Just want to add for others :
                                      for some reason, I didnt not get the cell that was visible to the user when I was scrolling to previous cell in collectionView with pagingEnabled.



                                      So I insert the code inside dispatch_async to give it some "air"
                                      and this works for me.



                                      -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
                                      {
                                      dispatch_async(dispatch_get_main_queue(), ^{
                                      UICollectionViewCell * visibleCell= [[self.collectionView visibleCells] objectAtIndex:0];


                                      [visibleCell doSomthing];
                                      });
                                      }





                                      share|improve this answer























                                      • This really helped! I didn't realized I can use dispatch_async block to make it absolutely flawless! This is the best answer!
                                        – kalafun
                                        Oct 20 '15 at 15:45






                                      • 1




                                        For Swift 4: DispatchQueue.main.async { when call initiates from something like func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
                                        – Jonny
                                        Dec 26 '17 at 10:33












                                      • Some clarification: this helped me too, and I had the same/similar problem, when scrolling back to uitableviewcell containing a uicollectionview. The refresh call initiated from willDisplay cell as mentioned above. I think the problem, somewhere in UIKit, is really the same in my case as this answer.
                                        – Jonny
                                        Dec 26 '17 at 10:39















                                      up vote
                                      8
                                      down vote













                                      Just want to add for others :
                                      for some reason, I didnt not get the cell that was visible to the user when I was scrolling to previous cell in collectionView with pagingEnabled.



                                      So I insert the code inside dispatch_async to give it some "air"
                                      and this works for me.



                                      -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
                                      {
                                      dispatch_async(dispatch_get_main_queue(), ^{
                                      UICollectionViewCell * visibleCell= [[self.collectionView visibleCells] objectAtIndex:0];


                                      [visibleCell doSomthing];
                                      });
                                      }





                                      share|improve this answer























                                      • This really helped! I didn't realized I can use dispatch_async block to make it absolutely flawless! This is the best answer!
                                        – kalafun
                                        Oct 20 '15 at 15:45






                                      • 1




                                        For Swift 4: DispatchQueue.main.async { when call initiates from something like func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
                                        – Jonny
                                        Dec 26 '17 at 10:33












                                      • Some clarification: this helped me too, and I had the same/similar problem, when scrolling back to uitableviewcell containing a uicollectionview. The refresh call initiated from willDisplay cell as mentioned above. I think the problem, somewhere in UIKit, is really the same in my case as this answer.
                                        – Jonny
                                        Dec 26 '17 at 10:39













                                      up vote
                                      8
                                      down vote










                                      up vote
                                      8
                                      down vote









                                      Just want to add for others :
                                      for some reason, I didnt not get the cell that was visible to the user when I was scrolling to previous cell in collectionView with pagingEnabled.



                                      So I insert the code inside dispatch_async to give it some "air"
                                      and this works for me.



                                      -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
                                      {
                                      dispatch_async(dispatch_get_main_queue(), ^{
                                      UICollectionViewCell * visibleCell= [[self.collectionView visibleCells] objectAtIndex:0];


                                      [visibleCell doSomthing];
                                      });
                                      }





                                      share|improve this answer














                                      Just want to add for others :
                                      for some reason, I didnt not get the cell that was visible to the user when I was scrolling to previous cell in collectionView with pagingEnabled.



                                      So I insert the code inside dispatch_async to give it some "air"
                                      and this works for me.



                                      -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
                                      {
                                      dispatch_async(dispatch_get_main_queue(), ^{
                                      UICollectionViewCell * visibleCell= [[self.collectionView visibleCells] objectAtIndex:0];


                                      [visibleCell doSomthing];
                                      });
                                      }






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Mar 22 '15 at 10:24

























                                      answered Mar 21 '15 at 21:54









                                      user1105951

                                      1,91922144




                                      1,91922144












                                      • This really helped! I didn't realized I can use dispatch_async block to make it absolutely flawless! This is the best answer!
                                        – kalafun
                                        Oct 20 '15 at 15:45






                                      • 1




                                        For Swift 4: DispatchQueue.main.async { when call initiates from something like func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
                                        – Jonny
                                        Dec 26 '17 at 10:33












                                      • Some clarification: this helped me too, and I had the same/similar problem, when scrolling back to uitableviewcell containing a uicollectionview. The refresh call initiated from willDisplay cell as mentioned above. I think the problem, somewhere in UIKit, is really the same in my case as this answer.
                                        – Jonny
                                        Dec 26 '17 at 10:39


















                                      • This really helped! I didn't realized I can use dispatch_async block to make it absolutely flawless! This is the best answer!
                                        – kalafun
                                        Oct 20 '15 at 15:45






                                      • 1




                                        For Swift 4: DispatchQueue.main.async { when call initiates from something like func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
                                        – Jonny
                                        Dec 26 '17 at 10:33












                                      • Some clarification: this helped me too, and I had the same/similar problem, when scrolling back to uitableviewcell containing a uicollectionview. The refresh call initiated from willDisplay cell as mentioned above. I think the problem, somewhere in UIKit, is really the same in my case as this answer.
                                        – Jonny
                                        Dec 26 '17 at 10:39
















                                      This really helped! I didn't realized I can use dispatch_async block to make it absolutely flawless! This is the best answer!
                                      – kalafun
                                      Oct 20 '15 at 15:45




                                      This really helped! I didn't realized I can use dispatch_async block to make it absolutely flawless! This is the best answer!
                                      – kalafun
                                      Oct 20 '15 at 15:45




                                      1




                                      1




                                      For Swift 4: DispatchQueue.main.async { when call initiates from something like func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
                                      – Jonny
                                      Dec 26 '17 at 10:33






                                      For Swift 4: DispatchQueue.main.async { when call initiates from something like func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
                                      – Jonny
                                      Dec 26 '17 at 10:33














                                      Some clarification: this helped me too, and I had the same/similar problem, when scrolling back to uitableviewcell containing a uicollectionview. The refresh call initiated from willDisplay cell as mentioned above. I think the problem, somewhere in UIKit, is really the same in my case as this answer.
                                      – Jonny
                                      Dec 26 '17 at 10:39




                                      Some clarification: this helped me too, and I had the same/similar problem, when scrolling back to uitableviewcell containing a uicollectionview. The refresh call initiated from willDisplay cell as mentioned above. I think the problem, somewhere in UIKit, is really the same in my case as this answer.
                                      – Jonny
                                      Dec 26 '17 at 10:39










                                      up vote
                                      8
                                      down vote













                                      It will probably be best to use UICollectionViewDelegate methods: (Swift 3)



                                      // Called before the cell is displayed    
                                      func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
                                      print(indexPath.row)
                                      }

                                      // Called when the cell is displayed
                                      func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
                                      print(indexPath.row)
                                      }





                                      share|improve this answer

















                                      • 2




                                        Regarding didEndDisplaying, from the docs: Tells the delegate that the specified cell was removed from the collection view. Use this method to detect when a cell is removed from a collection view, as opposed to monitoring the view itself to see when it disappears. So I do not think didEndDisplaying is called when the cell is displayed.
                                        – Jonny
                                        Dec 26 '17 at 10:49















                                      up vote
                                      8
                                      down vote













                                      It will probably be best to use UICollectionViewDelegate methods: (Swift 3)



                                      // Called before the cell is displayed    
                                      func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
                                      print(indexPath.row)
                                      }

                                      // Called when the cell is displayed
                                      func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
                                      print(indexPath.row)
                                      }





                                      share|improve this answer

















                                      • 2




                                        Regarding didEndDisplaying, from the docs: Tells the delegate that the specified cell was removed from the collection view. Use this method to detect when a cell is removed from a collection view, as opposed to monitoring the view itself to see when it disappears. So I do not think didEndDisplaying is called when the cell is displayed.
                                        – Jonny
                                        Dec 26 '17 at 10:49













                                      up vote
                                      8
                                      down vote










                                      up vote
                                      8
                                      down vote









                                      It will probably be best to use UICollectionViewDelegate methods: (Swift 3)



                                      // Called before the cell is displayed    
                                      func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
                                      print(indexPath.row)
                                      }

                                      // Called when the cell is displayed
                                      func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
                                      print(indexPath.row)
                                      }





                                      share|improve this answer












                                      It will probably be best to use UICollectionViewDelegate methods: (Swift 3)



                                      // Called before the cell is displayed    
                                      func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
                                      print(indexPath.row)
                                      }

                                      // Called when the cell is displayed
                                      func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
                                      print(indexPath.row)
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jun 20 '17 at 14:08









                                      Mr Stanev

                                      8081115




                                      8081115








                                      • 2




                                        Regarding didEndDisplaying, from the docs: Tells the delegate that the specified cell was removed from the collection view. Use this method to detect when a cell is removed from a collection view, as opposed to monitoring the view itself to see when it disappears. So I do not think didEndDisplaying is called when the cell is displayed.
                                        – Jonny
                                        Dec 26 '17 at 10:49














                                      • 2




                                        Regarding didEndDisplaying, from the docs: Tells the delegate that the specified cell was removed from the collection view. Use this method to detect when a cell is removed from a collection view, as opposed to monitoring the view itself to see when it disappears. So I do not think didEndDisplaying is called when the cell is displayed.
                                        – Jonny
                                        Dec 26 '17 at 10:49








                                      2




                                      2




                                      Regarding didEndDisplaying, from the docs: Tells the delegate that the specified cell was removed from the collection view. Use this method to detect when a cell is removed from a collection view, as opposed to monitoring the view itself to see when it disappears. So I do not think didEndDisplaying is called when the cell is displayed.
                                      – Jonny
                                      Dec 26 '17 at 10:49




                                      Regarding didEndDisplaying, from the docs: Tells the delegate that the specified cell was removed from the collection view. Use this method to detect when a cell is removed from a collection view, as opposed to monitoring the view itself to see when it disappears. So I do not think didEndDisplaying is called when the cell is displayed.
                                      – Jonny
                                      Dec 26 '17 at 10:49










                                      up vote
                                      7
                                      down vote














                                      For Swift 3.0




                                      func scrollViewDidScroll(_ scrollView: UIScrollView) {
                                      let visibleRect = CGRect(origin: colView.contentOffset, size: colView.bounds.size)
                                      let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
                                      let indexPath = colView.indexPathForItem(at: visiblePoint)
                                      }





                                      share|improve this answer



























                                        up vote
                                        7
                                        down vote














                                        For Swift 3.0




                                        func scrollViewDidScroll(_ scrollView: UIScrollView) {
                                        let visibleRect = CGRect(origin: colView.contentOffset, size: colView.bounds.size)
                                        let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
                                        let indexPath = colView.indexPathForItem(at: visiblePoint)
                                        }





                                        share|improve this answer

























                                          up vote
                                          7
                                          down vote










                                          up vote
                                          7
                                          down vote










                                          For Swift 3.0




                                          func scrollViewDidScroll(_ scrollView: UIScrollView) {
                                          let visibleRect = CGRect(origin: colView.contentOffset, size: colView.bounds.size)
                                          let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
                                          let indexPath = colView.indexPathForItem(at: visiblePoint)
                                          }





                                          share|improve this answer















                                          For Swift 3.0




                                          func scrollViewDidScroll(_ scrollView: UIScrollView) {
                                          let visibleRect = CGRect(origin: colView.contentOffset, size: colView.bounds.size)
                                          let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
                                          let indexPath = colView.indexPathForItem(at: visiblePoint)
                                          }






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited May 17 '17 at 11:58









                                          Varun Naharia

                                          2,68442850




                                          2,68442850










                                          answered Jan 28 '17 at 12:23









                                          Hiren Panchal

                                          1,7491518




                                          1,7491518






















                                              up vote
                                              5
                                              down vote













                                              Swift 3.0



                                              Simplest solution which will give you indexPath for visible cells..



                                              yourCollectionView.indexPathsForVisibleItems 


                                              will return the array of indexpath.



                                              Just take the first object from array like this.



                                              yourCollectionView.indexPathsForVisibleItems.first


                                              I guess it should work fine with Objective - C as well.






                                              share|improve this answer



























                                                up vote
                                                5
                                                down vote













                                                Swift 3.0



                                                Simplest solution which will give you indexPath for visible cells..



                                                yourCollectionView.indexPathsForVisibleItems 


                                                will return the array of indexpath.



                                                Just take the first object from array like this.



                                                yourCollectionView.indexPathsForVisibleItems.first


                                                I guess it should work fine with Objective - C as well.






                                                share|improve this answer

























                                                  up vote
                                                  5
                                                  down vote










                                                  up vote
                                                  5
                                                  down vote









                                                  Swift 3.0



                                                  Simplest solution which will give you indexPath for visible cells..



                                                  yourCollectionView.indexPathsForVisibleItems 


                                                  will return the array of indexpath.



                                                  Just take the first object from array like this.



                                                  yourCollectionView.indexPathsForVisibleItems.first


                                                  I guess it should work fine with Objective - C as well.






                                                  share|improve this answer














                                                  Swift 3.0



                                                  Simplest solution which will give you indexPath for visible cells..



                                                  yourCollectionView.indexPathsForVisibleItems 


                                                  will return the array of indexpath.



                                                  Just take the first object from array like this.



                                                  yourCollectionView.indexPathsForVisibleItems.first


                                                  I guess it should work fine with Objective - C as well.







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited May 29 '17 at 5:11

























                                                  answered May 29 '17 at 5:05









                                                  Anil Kukadeja

                                                  7711923




                                                  7711923






















                                                      up vote
                                                      2
                                                      down vote













                                                      You can use scrollViewDidEndDecelerating: for this



                                                      //@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

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

                                                      for (UICollectionViewCell *cell in [self.collectionView visibleCells]) {
                                                      NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
                                                      NSUInteger lastIndex = [indexPath indexAtPosition:[indexPath length] - 1];
                                                      NSLog(@"visible cell value %d",lastIndex);
                                                      }

                                                      }





                                                      share|improve this answer



























                                                        up vote
                                                        2
                                                        down vote













                                                        You can use scrollViewDidEndDecelerating: for this



                                                        //@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

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

                                                        for (UICollectionViewCell *cell in [self.collectionView visibleCells]) {
                                                        NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
                                                        NSUInteger lastIndex = [indexPath indexAtPosition:[indexPath length] - 1];
                                                        NSLog(@"visible cell value %d",lastIndex);
                                                        }

                                                        }





                                                        share|improve this answer

























                                                          up vote
                                                          2
                                                          down vote










                                                          up vote
                                                          2
                                                          down vote









                                                          You can use scrollViewDidEndDecelerating: for this



                                                          //@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

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

                                                          for (UICollectionViewCell *cell in [self.collectionView visibleCells]) {
                                                          NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
                                                          NSUInteger lastIndex = [indexPath indexAtPosition:[indexPath length] - 1];
                                                          NSLog(@"visible cell value %d",lastIndex);
                                                          }

                                                          }





                                                          share|improve this answer














                                                          You can use scrollViewDidEndDecelerating: for this



                                                          //@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

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

                                                          for (UICollectionViewCell *cell in [self.collectionView visibleCells]) {
                                                          NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
                                                          NSUInteger lastIndex = [indexPath indexAtPosition:[indexPath length] - 1];
                                                          NSLog(@"visible cell value %d",lastIndex);
                                                          }

                                                          }






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Oct 15 '14 at 13:39

























                                                          answered Oct 15 '14 at 13:28









                                                          raaz

                                                          7,212195479




                                                          7,212195479






















                                                              up vote
                                                              2
                                                              down vote













                                                              UICollectionView current visible cell index: Swift 3



                                                              var visibleCurrentCellIndexPath: IndexPath? {
                                                              for cell in self.collectionView.visibleCells {
                                                              let indexPath = self.collectionView.indexPath(for: cell)
                                                              return indexPath
                                                              }

                                                              return nil
                                                              }





                                                              share|improve this answer























                                                              • Best answer. Clean and a few lines.
                                                                – garanda
                                                                Dec 1 '16 at 8:45















                                                              up vote
                                                              2
                                                              down vote













                                                              UICollectionView current visible cell index: Swift 3



                                                              var visibleCurrentCellIndexPath: IndexPath? {
                                                              for cell in self.collectionView.visibleCells {
                                                              let indexPath = self.collectionView.indexPath(for: cell)
                                                              return indexPath
                                                              }

                                                              return nil
                                                              }





                                                              share|improve this answer























                                                              • Best answer. Clean and a few lines.
                                                                – garanda
                                                                Dec 1 '16 at 8:45













                                                              up vote
                                                              2
                                                              down vote










                                                              up vote
                                                              2
                                                              down vote









                                                              UICollectionView current visible cell index: Swift 3



                                                              var visibleCurrentCellIndexPath: IndexPath? {
                                                              for cell in self.collectionView.visibleCells {
                                                              let indexPath = self.collectionView.indexPath(for: cell)
                                                              return indexPath
                                                              }

                                                              return nil
                                                              }





                                                              share|improve this answer














                                                              UICollectionView current visible cell index: Swift 3



                                                              var visibleCurrentCellIndexPath: IndexPath? {
                                                              for cell in self.collectionView.visibleCells {
                                                              let indexPath = self.collectionView.indexPath(for: cell)
                                                              return indexPath
                                                              }

                                                              return nil
                                                              }






                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited Mar 22 at 19:03









                                                              Nik Kov

                                                              2,9762450




                                                              2,9762450










                                                              answered Nov 24 '16 at 12:26









                                                              Mihail Salari

                                                              66249




                                                              66249












                                                              • Best answer. Clean and a few lines.
                                                                – garanda
                                                                Dec 1 '16 at 8:45


















                                                              • Best answer. Clean and a few lines.
                                                                – garanda
                                                                Dec 1 '16 at 8:45
















                                                              Best answer. Clean and a few lines.
                                                              – garanda
                                                              Dec 1 '16 at 8:45




                                                              Best answer. Clean and a few lines.
                                                              – garanda
                                                              Dec 1 '16 at 8:45










                                                              up vote
                                                              1
                                                              down vote













                                                              converting @Anthony's answer to Swift 3.0 worked perfectly for me:



                                                              func scrollViewDidScroll(_ scrollView: UIScrollView) {

                                                              var visibleRect = CGRect()
                                                              visibleRect.origin = yourCollectionView.contentOffset
                                                              visibleRect.size = yourCollectionView.bounds.size
                                                              let visiblePoint = CGPoint(x: CGFloat(visibleRect.midX), y: CGFloat(visibleRect.midY))
                                                              let visibleIndexPath: IndexPath? = yourCollectionView.indexPathForItem(at: visiblePoint)
                                                              print("Visible cell's index is : (visibleIndexPath?.row)!")
                                                              }





                                                              share|improve this answer

























                                                                up vote
                                                                1
                                                                down vote













                                                                converting @Anthony's answer to Swift 3.0 worked perfectly for me:



                                                                func scrollViewDidScroll(_ scrollView: UIScrollView) {

                                                                var visibleRect = CGRect()
                                                                visibleRect.origin = yourCollectionView.contentOffset
                                                                visibleRect.size = yourCollectionView.bounds.size
                                                                let visiblePoint = CGPoint(x: CGFloat(visibleRect.midX), y: CGFloat(visibleRect.midY))
                                                                let visibleIndexPath: IndexPath? = yourCollectionView.indexPathForItem(at: visiblePoint)
                                                                print("Visible cell's index is : (visibleIndexPath?.row)!")
                                                                }





                                                                share|improve this answer























                                                                  up vote
                                                                  1
                                                                  down vote










                                                                  up vote
                                                                  1
                                                                  down vote









                                                                  converting @Anthony's answer to Swift 3.0 worked perfectly for me:



                                                                  func scrollViewDidScroll(_ scrollView: UIScrollView) {

                                                                  var visibleRect = CGRect()
                                                                  visibleRect.origin = yourCollectionView.contentOffset
                                                                  visibleRect.size = yourCollectionView.bounds.size
                                                                  let visiblePoint = CGPoint(x: CGFloat(visibleRect.midX), y: CGFloat(visibleRect.midY))
                                                                  let visibleIndexPath: IndexPath? = yourCollectionView.indexPathForItem(at: visiblePoint)
                                                                  print("Visible cell's index is : (visibleIndexPath?.row)!")
                                                                  }





                                                                  share|improve this answer












                                                                  converting @Anthony's answer to Swift 3.0 worked perfectly for me:



                                                                  func scrollViewDidScroll(_ scrollView: UIScrollView) {

                                                                  var visibleRect = CGRect()
                                                                  visibleRect.origin = yourCollectionView.contentOffset
                                                                  visibleRect.size = yourCollectionView.bounds.size
                                                                  let visiblePoint = CGPoint(x: CGFloat(visibleRect.midX), y: CGFloat(visibleRect.midY))
                                                                  let visibleIndexPath: IndexPath? = yourCollectionView.indexPathForItem(at: visiblePoint)
                                                                  print("Visible cell's index is : (visibleIndexPath?.row)!")
                                                                  }






                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered May 10 '17 at 7:08









                                                                  Nilesh Pol

                                                                  6801925




                                                                  6801925






















                                                                      up vote
                                                                      0
                                                                      down vote













                                                                      This is old question but in my case...



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

                                                                      _m_offsetIdx = [m_cv indexPathForCell:m_cv.visibleCells.firstObject].row;
                                                                      }

                                                                      - (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
                                                                      _m_offsetIdx = [m_cv indexPathForCell:m_cv.visibleCells.lastObject].row;
                                                                      }





                                                                      share|improve this answer

























                                                                        up vote
                                                                        0
                                                                        down vote













                                                                        This is old question but in my case...



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

                                                                        _m_offsetIdx = [m_cv indexPathForCell:m_cv.visibleCells.firstObject].row;
                                                                        }

                                                                        - (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
                                                                        _m_offsetIdx = [m_cv indexPathForCell:m_cv.visibleCells.lastObject].row;
                                                                        }





                                                                        share|improve this answer























                                                                          up vote
                                                                          0
                                                                          down vote










                                                                          up vote
                                                                          0
                                                                          down vote









                                                                          This is old question but in my case...



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

                                                                          _m_offsetIdx = [m_cv indexPathForCell:m_cv.visibleCells.firstObject].row;
                                                                          }

                                                                          - (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
                                                                          _m_offsetIdx = [m_cv indexPathForCell:m_cv.visibleCells.lastObject].row;
                                                                          }





                                                                          share|improve this answer












                                                                          This is old question but in my case...



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

                                                                          _m_offsetIdx = [m_cv indexPathForCell:m_cv.visibleCells.firstObject].row;
                                                                          }

                                                                          - (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
                                                                          _m_offsetIdx = [m_cv indexPathForCell:m_cv.visibleCells.lastObject].row;
                                                                          }






                                                                          share|improve this answer












                                                                          share|improve this answer



                                                                          share|improve this answer










                                                                          answered May 23 '16 at 14:00









                                                                          Hwangho Kim

                                                                          392211




                                                                          392211






















                                                                              up vote
                                                                              0
                                                                              down vote













                                                                              Also check this snippet



                                                                              let isCellVisible = collectionView.visibleCells.map { collectionView.indexPath(for: $0) }.contains(inspectingIndexPath)





                                                                              share|improve this answer

























                                                                                up vote
                                                                                0
                                                                                down vote













                                                                                Also check this snippet



                                                                                let isCellVisible = collectionView.visibleCells.map { collectionView.indexPath(for: $0) }.contains(inspectingIndexPath)





                                                                                share|improve this answer























                                                                                  up vote
                                                                                  0
                                                                                  down vote










                                                                                  up vote
                                                                                  0
                                                                                  down vote









                                                                                  Also check this snippet



                                                                                  let isCellVisible = collectionView.visibleCells.map { collectionView.indexPath(for: $0) }.contains(inspectingIndexPath)





                                                                                  share|improve this answer












                                                                                  Also check this snippet



                                                                                  let isCellVisible = collectionView.visibleCells.map { collectionView.indexPath(for: $0) }.contains(inspectingIndexPath)






                                                                                  share|improve this answer












                                                                                  share|improve this answer



                                                                                  share|improve this answer










                                                                                  answered May 22 at 7:37









                                                                                  Nik Kov

                                                                                  2,9762450




                                                                                  2,9762450






















                                                                                      up vote
                                                                                      0
                                                                                      down vote













                                                                                      Swift 3 & Swift 4:



                                                                                      func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
                                                                                      var visibleRect = CGRect()

                                                                                      visibleRect.origin = collectionView.contentOffset
                                                                                      visibleRect.size = collectionView.bounds.size

                                                                                      let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)

                                                                                      guard let indexPath = collectionView.indexPathForItem(at: visiblePoint) else { return }

                                                                                      print(indexPath[1])
                                                                                      }


                                                                                      If you want to show actual number than you can add +1






                                                                                      share|improve this answer

























                                                                                        up vote
                                                                                        0
                                                                                        down vote













                                                                                        Swift 3 & Swift 4:



                                                                                        func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
                                                                                        var visibleRect = CGRect()

                                                                                        visibleRect.origin = collectionView.contentOffset
                                                                                        visibleRect.size = collectionView.bounds.size

                                                                                        let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)

                                                                                        guard let indexPath = collectionView.indexPathForItem(at: visiblePoint) else { return }

                                                                                        print(indexPath[1])
                                                                                        }


                                                                                        If you want to show actual number than you can add +1






                                                                                        share|improve this answer























                                                                                          up vote
                                                                                          0
                                                                                          down vote










                                                                                          up vote
                                                                                          0
                                                                                          down vote









                                                                                          Swift 3 & Swift 4:



                                                                                          func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
                                                                                          var visibleRect = CGRect()

                                                                                          visibleRect.origin = collectionView.contentOffset
                                                                                          visibleRect.size = collectionView.bounds.size

                                                                                          let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)

                                                                                          guard let indexPath = collectionView.indexPathForItem(at: visiblePoint) else { return }

                                                                                          print(indexPath[1])
                                                                                          }


                                                                                          If you want to show actual number than you can add +1






                                                                                          share|improve this answer












                                                                                          Swift 3 & Swift 4:



                                                                                          func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
                                                                                          var visibleRect = CGRect()

                                                                                          visibleRect.origin = collectionView.contentOffset
                                                                                          visibleRect.size = collectionView.bounds.size

                                                                                          let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)

                                                                                          guard let indexPath = collectionView.indexPathForItem(at: visiblePoint) else { return }

                                                                                          print(indexPath[1])
                                                                                          }


                                                                                          If you want to show actual number than you can add +1







                                                                                          share|improve this answer












                                                                                          share|improve this answer



                                                                                          share|improve this answer










                                                                                          answered Nov 11 at 7:01









                                                                                          Jamil Hasnine Tamim

                                                                                          1,445820




                                                                                          1,445820






















                                                                                              up vote
                                                                                              -1
                                                                                              down vote













                                                                                              try this, it works. (in the example below i have 3 cells for example.)



                                                                                                  func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
                                                                                              let visibleRect = CGRect(origin: self.collectionView.contentOffset, size: self.collectionView.bounds.size)
                                                                                              let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))
                                                                                              let visibleIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)
                                                                                              if let v = visibleIndexPath {
                                                                                              switch v.item {
                                                                                              case 0: setImageDescription()
                                                                                              break
                                                                                              case 1: setImageConditions()
                                                                                              break
                                                                                              case 2: setImageResults()
                                                                                              break
                                                                                              default: break
                                                                                              }
                                                                                              }





                                                                                              share|improve this answer

























                                                                                                up vote
                                                                                                -1
                                                                                                down vote













                                                                                                try this, it works. (in the example below i have 3 cells for example.)



                                                                                                    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
                                                                                                let visibleRect = CGRect(origin: self.collectionView.contentOffset, size: self.collectionView.bounds.size)
                                                                                                let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))
                                                                                                let visibleIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)
                                                                                                if let v = visibleIndexPath {
                                                                                                switch v.item {
                                                                                                case 0: setImageDescription()
                                                                                                break
                                                                                                case 1: setImageConditions()
                                                                                                break
                                                                                                case 2: setImageResults()
                                                                                                break
                                                                                                default: break
                                                                                                }
                                                                                                }





                                                                                                share|improve this answer























                                                                                                  up vote
                                                                                                  -1
                                                                                                  down vote










                                                                                                  up vote
                                                                                                  -1
                                                                                                  down vote









                                                                                                  try this, it works. (in the example below i have 3 cells for example.)



                                                                                                      func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
                                                                                                  let visibleRect = CGRect(origin: self.collectionView.contentOffset, size: self.collectionView.bounds.size)
                                                                                                  let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))
                                                                                                  let visibleIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)
                                                                                                  if let v = visibleIndexPath {
                                                                                                  switch v.item {
                                                                                                  case 0: setImageDescription()
                                                                                                  break
                                                                                                  case 1: setImageConditions()
                                                                                                  break
                                                                                                  case 2: setImageResults()
                                                                                                  break
                                                                                                  default: break
                                                                                                  }
                                                                                                  }





                                                                                                  share|improve this answer












                                                                                                  try this, it works. (in the example below i have 3 cells for example.)



                                                                                                      func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
                                                                                                  let visibleRect = CGRect(origin: self.collectionView.contentOffset, size: self.collectionView.bounds.size)
                                                                                                  let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))
                                                                                                  let visibleIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)
                                                                                                  if let v = visibleIndexPath {
                                                                                                  switch v.item {
                                                                                                  case 0: setImageDescription()
                                                                                                  break
                                                                                                  case 1: setImageConditions()
                                                                                                  break
                                                                                                  case 2: setImageResults()
                                                                                                  break
                                                                                                  default: break
                                                                                                  }
                                                                                                  }






                                                                                                  share|improve this answer












                                                                                                  share|improve this answer



                                                                                                  share|improve this answer










                                                                                                  answered Nov 22 '16 at 5:21









                                                                                                  Arash Jamshidi

                                                                                                  5316




                                                                                                  5316






























                                                                                                      draft saved

                                                                                                      draft discarded




















































                                                                                                      Thanks for contributing an answer to Stack Overflow!


                                                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                                                      But avoid



                                                                                                      • Asking for help, clarification, or responding to other answers.

                                                                                                      • Making statements based on opinion; back them up with references or personal experience.


                                                                                                      To learn more, see our tips on writing great answers.





                                                                                                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                                                                      Please pay close attention to the following guidance:


                                                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                                                      But avoid



                                                                                                      • Asking for help, clarification, or responding to other answers.

                                                                                                      • Making statements based on opinion; back them up with references or personal experience.


                                                                                                      To learn more, see our tips on writing great answers.




                                                                                                      draft saved


                                                                                                      draft discarded














                                                                                                      StackExchange.ready(
                                                                                                      function () {
                                                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18649920%2fuicollectionview-current-visible-cell-index%23new-answer', 'question_page');
                                                                                                      }
                                                                                                      );

                                                                                                      Post as a guest















                                                                                                      Required, but never shown





















































                                                                                                      Required, but never shown














                                                                                                      Required, but never shown












                                                                                                      Required, but never shown







                                                                                                      Required, but never shown

































                                                                                                      Required, but never shown














                                                                                                      Required, but never shown












                                                                                                      Required, but never shown







                                                                                                      Required, but never shown







                                                                                                      Popular posts from this blog

                                                                                                      Florida Star v. B. J. F.

                                                                                                      Error while running script in elastic search , gateway timeout

                                                                                                      Adding quotations to stringified JSON object values