【问题标题】:How to trigger refresh in Supplementary view of UICollectionView如何在 UICollectionView 的补充视图中触发刷新
【发布时间】: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


【解决方案1】:

使用UIApplicationSignificantTimeChangeNotification 通知您时间变化。

使用NotificationCenter 来实现这一点。像这样的:

NotificationCenter.defaultCenter().addObserver(self, selector: "dayChanged:", name: UIApplicationSignificantTimeChangeNotification, object: nil)

那么你需要一个函数来处理变化:

func dayChanged(notification: NSNotification){

    // trigger the changes you need here
}

【讨论】:

  • 在dayChanged中,如何重绘节标题?
  • 您的收藏视图可能会有一个IBOutlet var - 也许是:myCollectionView。然后调用myCollectionView.reloadData() 就可以了。
  • 对于仅重新加载标题,您可以使用collectionView reloadSections。您需要知道要更新的索引范围。
猜你喜欢
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 2013-02-20
  • 2015-12-22
  • 1970-01-01
  • 2016-07-23
  • 2015-06-16
  • 1970-01-01
相关资源
最近更新 更多