I'm stuck while working on a custom calendar. Each day is within an Array
and then put into custom CollectionViewCells
.
Within the cell of the current day, I want to put a kind of "marker" (I use a UIProgressBar
for this) to show the progress of the day. Means at noon of today, the UIProgressBar
should be at 0.5 and so on.
To handle this, I have the following function which return a Float with one decimal to use it in the UIProgressBar
:
func getPercentage() -> Float {
var cal = Calendar(identifier: .gregorian)
cal.timeZone = TimeZone(abbreviation: "UTC")!
let hours = cal.component(.hour, from: Date())
let minutes = cal.component(.minute, from: Date())
let allHours = Float(hours) + (Float(minutes) / 60)
let result = round((allTimeInHours / 24) * 10) / 10
return allTimeInPercent
}
Within the cellForItemAt indexPath
im checking if the cell is the cell of today, and if it's the case, I'm calling the getPercentage()
method to set the UIProgressBar
within the cell accordingly:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.identifier, for: indexPath) as! MyCollectionViewCell
//other functionality + cell-setup
if dateArray[indexPath.item] == todayAsDate() {
cell.progressBar.isHidden = false
cell.progressBar.progress = getPercentageOfDay()
} else {
cell.progressBar.isHidden = true
}
return cell
}
So far everything works fine and looks like this:
However whenever I go to the Home Screen and then back to the app, the UIProgressBar
is always set to 1.0.
After completely closing out the app via the AppSwitcher and restart the app, everything works fine again.
Im guessing there's a problem because I use dequeueReusableCells
but im not really getting behind it. I don't know where the 1.0 for the UIProgressBar
is coming from, even when I change the default in the Storyboard the result is the same.
Also if anyone has a suggestion to use something else than a UIProgressBar
im open for any ideas. Actually I would prefer to just have vertical line moving left to right according to the time of the day.
I'm pretty much a beginner and happy for any hints! :)