0

I have variable startTime and endTime. In the collectionCell me need to display the start time and end time, but I need to between the start time and the end time was still the time range.

For example:

The day has 24 hours. My startTime is 06:00 and my endTime is 13:00. Me need to display range from 06:00 to 13:00 and in collectionCell i want to see it so:

06:00

07:00

08:00

09:00

10:00

11:00

12:00

13:00

Can I have it display? And how to do it?

My code:

var startTime: String = "06:00"
var endTime: String = "13:00"

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return // what need is there to write?
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! BookingTimeCell
    cell.timeLabel.text = // what need is there to write?
    return cell
}
  • This might help as a starting point: [Print all dates between two NSDate()](https://stackoverflow.com/questions/32536612/swift-print-all-dates-between-two-nsdate) – Martin R Jan 06 '18 at 09:57
  • If you only have to display a range, just put your hours in one array using a for loop. In numberOfItemsInSection return yourArray.count, in cellForItemAt use cell.timeLabel.text = yourArray[indexPath.item] – Andrea Toso Jan 06 '18 at 10:55
  • @AndreaToso thanks, i understand, but how to use loop? – Дмитрий Деникаев Jan 06 '18 at 11:30

1 Answers1

1

Programming often times is about breaking a large problem into smaller pieces. Here are the issues you were facing:

  1. Your time is stored as a String. You can't add an hour to a string. Need to convert it into a Date
  2. You need to generate an array of hours in between startTime and endTime and update this array whenever startTime or endTime changes.
  3. Finally, show this array in the collection view.

Here's one way to do it:

class ViewController: UIViewController, UICollectionViewDataSource {
    @IBOutlet weak var collectionView: UICollectionView!

    var startTime: String = "06:00"
    var endTime: String = "13:00"
    var timeRange = [String]()      // this array contains all the hours between your startTime and endTime

    // The formatter is responsible for converting a String to a Date and vice-versa
    var timeFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "HH:mm"
        return formatter
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Every time startTime and endTime change, reload the collection view
        // with a code block like this
        if let timeRange = generateTimeRange() {
            self.timeRange = timeRange
            self.collectionView.reloadData()
        } else {
            // Handle error
        }

        self.collectionView.dataSource = self
    }

    // Call this function in viewDidLoad and whenever startTime or endTime change
    // Note this function returns an empty array if the time range crosses the day
    // boundary, for example: startTime = 22:00, endTime = 02:00
    func generateTimeRange() -> [String]? {
        var result = [String]()
        guard var startDate = timeFormatter.date(from: startTime) else { return nil }
        guard let endDate   = timeFormatter.date(from: endTime) else { return nil }

        while startDate <= endDate {
            result.append(timeFormatter.string(from: startDate))
            startDate = Calendar.current.date(byAdding: .hour, value: 1, to: startDate)!
        }

        return result
    }

    // MARK: - UICollectionViewDataSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return timeRange.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! BookingTimeCell
        cell.timeLabel.text = timeRange[indexPath.row]
        return cell
    }
}

Result:

enter image description here

Code Different
  • 90,614
  • 16
  • 144
  • 163