【发布时间】:2017-06-30 14:06:18
【问题描述】:
我们正在使用 UICollectionView 来绘制日历。补充视图用于表示日历中某一天的列顶部的日期。每个日历都是 UICollectionView 中的一个部分。我们希望突出显示当天。我们有这个工作
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!
}
这个
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
}
}
}
在我第一次进入这个视图时设置我们想要的背景。但是如果我打开了这个视图并且日期发生了变化,我该如何更新标题?我应该在哪里移动设置日期的逻辑,以及如何“无效”或我需要做的任何事情?
【问题讨论】:
-
日期因用户交互而改变?
-
invalidate 似乎没有触发重绘补充视图。这是一个限制还是我没有正确接线?
-
如果由于用户交互(例如用户点击单元格)需要更改日期,则需要实现
didSelectItemAtIndexPath方法。 -
我想根据时间变化来改变它。用户打开它,现在时间已经过了午夜。我希望标题视图现在看起来不同,因为它代表今天。我需要把前一个的背景颜色去掉,然后把它加到右边
标签: ios swift uicollectionview uicollectionreusableview