0

I am creating a small application that displays the bus timetables of my city. I use a UICollectionView to display all the timetables:

From 06:00 in the morning to 00:30 in the evening. Buses pass every 30 min.

I would like my cells to scroll automatically according to the time of the day, even if the application is closed. I would like my the cells that display the next bus (e.g. 2:30 pm) display the one 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 would like to display the cell that shows 06:00. And the process continues ad infinitum.

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])
    
  }
}
Roland Lariotte
  • 2,606
  • 1
  • 16
  • 40
fardi Clk
  • 1
  • 2

1 Answers1

0

1- the solution I propose is you create a function that runs when opening the application, with it you capture the current time and then you adjust the data of your collection to the current time.

2- after that you can run an asynchronous function to synchronize with the current time and make this update while the application is running think about this code base for that

 Timer.scheduledTimer(withTimeInterval: TimeInterval, repeats: true) { timer in
        ...
    }
  • 1
    Thanks for your reply, but how do you adjust the data of the collection to the current time ? Sorry, I'm a complete novice in swift. – fardi Clk Aug 28 '21 at 15:13
  • I think this can be useful [link](https://stackoverflow.com/questions/24070450/how-to-get-the-current-time-as-datetime) – Guilherme Rangel Sep 06 '21 at 18:41