How to scroll the Collectionview that to the selected date in the index iOS swift

 Problem:

I'm having one date with me.I had binded the data list to the tableView.I want to to scroll and view the date indexed data to the user.Also if next date event available then scroll to that index.How to achieve that?

Solution:

    func gotoTodayIndex(selectedDate: Date) {

        if let indexPath = myEventDataSource.eventItems.firstIndex(where:{selectedDate == $0.date}) {

            collView.scrollToItem(atIndexPath(item: 0, section: indexPath), at: .centeredVerticallyanimatedtrue)

        } else {

            if let nextindexPath = myEventDataSource.eventItems.firstIndex(where:{$0.date ?? Date() > selectedDate}) {

                collView.scrollToItem(at: IndexPath(item: 0, section: nextindexPath), at: .centeredVertically, animated: true)

            }

        }

    }


The above function will be act as if selected date contains event in the tableview then scroll and position to that index of scrollview.


If that date not contains any event then next available event date will be focused centre of the screen to be scrolled.

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift