[英]How to trigger refresh in Supplementary view of UICollectionView
We are using UICollectionView to draw a calendar. 我们正在使用UICollectionView绘制日历。 The supplementary view is used for the date at the top of a column that represents a day in the calendar. 补充视图用于代表日历中一天的列顶部的日期。 Each calendar is a section in the UICollectionView. 每个日历都是UICollectionView中的一个部分。 We want the current day highlighted. 我们希望突出显示当天。 We have that working by 我们有工作
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
var view: UICollectionReusableView?
if kind == WeeklyCollectionElementKindDayColumnHeader {
let dayColumnHeader: WeeklyDayColumnHeader? = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: WeeklyDayColumnHeaderReuseIdentifier, for: indexPath) as? WeeklyDayColumnHeader
let day: Date? = collectionViewCalendarLayout?.dateForDayColumnHeader(at: indexPath)
let currentDay: Date? = currentTimeComponents(for: self.collectionView!, layout: collectionViewCalendarLayout!)
let startOfDay: Date? = Calendar.current.startOfDay(for: day!)
let startOfCurrentDay: Date? = Calendar.current.startOfDay(for: currentDay!)
dayColumnHeader?.day = day
dayColumnHeader?.isCurrentDay = startOfDay == startOfCurrentDay
view = dayColumnHeader
}
else if kind == WeeklyCollectionElementKindTimeRowHeader {
let timeRowHeader: WeeklyTimeRowHeader? = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: WeeklyTimeRowHeaderReuseIdentifier, for: indexPath) as? WeeklyTimeRowHeader
timeRowHeader?.time = collectionViewCalendarLayout?.dateForTimeRowHeader(at: indexPath)
view = timeRowHeader
}
return view!
}
which with this 与此
class WeeklyDayColumnHeader: UICollectionReusableView {
var day: Date? {
didSet {
var dateFormatter: DateFormatter?
if dateFormatter == nil {
dateFormatter = DateFormatter()
dateFormatter?.dateFormat = "E d"
}
self.title!.text = dateFormatter?.string(from: day!)
setNeedsLayout()
}
}
var isCurrentDay: Bool = false {
didSet {
if isCurrentDay {
title?.textColor = UIColor.white
title?.font = UIFont.boldSystemFont(ofSize: CGFloat(16.0))
titleBackground?.backgroundColor = UIColor(red:0.99, green:0.22, blue:0.21, alpha:1.0)
}
else {
title?.font = UIFont.systemFont(ofSize: CGFloat(16.0))
title?.textColor = UIColor.black
titleBackground?.backgroundColor = UIColor.clear
}
}
}
set the background we want the first time I come into this view. 设置我们希望我第一次进入此视图的背景。 But if I have this view open and the date changes, how do I update the header? 但是,如果我打开此视图并且日期更改了,如何更新标题? Where should I move the logic that sets the date, and how do I "invalidate" or whatever I need to do? 我应该将设置日期的逻辑移到哪里,如何使“无效”或需要执行的任何操作?
Use the UIApplicationSignificantTimeChangeNotification
in order to notify you of time changes. 使用UIApplicationSignificantTimeChangeNotification
可以将时间更改通知您。
Use the NotificationCenter
to implement this. 使用NotificationCenter
来实现这一点。 Something like this: 像这样:
NotificationCenter.defaultCenter().addObserver(self, selector: "dayChanged:", name: UIApplicationSignificantTimeChangeNotification, object: nil)
Then you need a function to handle the change: 然后,您需要一个函数来处理更改:
func dayChanged(notification: NSNotification){
// trigger the changes you need here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.