How to scroll UICollectionView cell automatically according to the time of day

Hello, Can you help me please ? I am creating a small application that displays the bus timetables of my city. I use UICollectionView to display all the timetables: from 06:00 in the morning to 00:30 in the evening. Buses pass every 30 min. Now I want my cells to scroll automatically according to the time of day, even if the application is closed. I want my cell that displays the next bus (e.g. 2:30 pm) to display at 2:00 pm and automatically scroll after 2:30 pm to display the next bus that comes at 3:00 pm etc. until 00:30 am. And after 00:30, I want to display the cell that shows 06:00). And the process continues ad infinitum. thank you

There my code :

`extension HomeViewController : UICollectionViewDelegate, UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return busHoursList.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
let busCell = collectionView.dequeueReusableCell(withReuseIdentifier: "busCellID", for: indexPath) as! BusCollectionViewCell
        busCell.layer.borderWidth = 0.6
        busCell.layer.cornerRadius = 15
        busCell.layer.borderColor = CGColor.init(red: 170/250, green: 170/250, blue: 170/250, alpha: 1)
        busCell.setupDepartureHours(withText: busDepartureHoursList[indexPath.row])}
     

Can you clarify what you mean by even if the application is closed? When the application is closed, no collection view is shown so scroll automatically does not make sense.

excuse me. I meant, when the user launches the application, I want to have the cell that corresponds to the time of the next bus. Do you understand better like this?

Thanks for the additional explanation. I believe I understand far better. You may need to call scrollToItem at launch time and at every minute (every second?) the next bus is changed.

You could use local notification.

To illustrate:

  • bus 1 leaves at 8:00
  • bus 2 leaves at 8:30
  • bus 3 leaves at 9:00

It is 7:45

  • bus 1 shows at top of scrollView
  • you post a notification to trigger at 8:00 (when bus 1 has departed)

It is 8:00

  • the action of notification will scrollToItem 2 : the next bus in the timetable after 8:00
  • and post a new notification to fire at 8:30

It is 8:30

  • the action of notification will scrollToItem 3 : the next bus in the timetable after 8:30

etc…

See: https://stackoverflow.com/questions/52009454/how-do-i-send-local-notifications-at-a-specific-time-in-swift

You don't replace the content of the notification but schedule a new one at the desired time.

You don't change the action:

  • at the present time (when notification has been fired), search for the next bus to depart
  • scroll to the corresponding item

Here my code for the Local Notification :

 let center = UNUserNotificationCenter.current()


        let content = UNMutableNotificationContent()

        content.title = NSString.localizedUserNotificationString(forKey: "New Notif", arguments: nil)

        content.body = NSString.localizedUserNotificationString(forKey: "empty", arguments: nil)

        content.badge = 1


        var date = DateComponents()

        date.hour = 8

        date.minute = 00

        date.timeZone = TimeZone.current


        let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)


        let uuidString = UUID().uuidString

        let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger)


        center.add(request) { (error) in

            if error != nil {

                print("Error \(String(describing: error?.localizedDescription))")

            } else {

                print("✅ ✅ ✅ Send !")

            }

        }

How to scroll UICollectionView cell automatically according to the time of day
 
 
Q