如何将数据从集合视图标头传递到集合视图

问题描述

我有一个带有标题和正文的集合视图。

页眉具有一个将日期增加1个月的按钮。

我需要标题中的按钮还可以增加集合视图正文中的日期。

这是基本代码

class ExampleViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        NotificationCenter.default.addobserver(self,selector: #selector(self.refresh),name: NSNotification.Name(rawValue: "dateUpdated"),object: nil)

        }
                
        @objc private func refresh() {
            collectionView.reloadData()
        }
    
    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        return categories.count
    }
    
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ExampleCell",for: indexPath) as! ExampleCell
        
        cell.configureCell(with: indexPath)
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView,viewForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind,withReuseIdentifier: "ExampleHeader",for: indexPath) as! ExampleHeader
        
        header.configureHeader()

        return header
    }
    
}
class ExampleHeader: UICollectionReusableView {
    
    var selectedDate = Date()
    
    func configureHeader() {
        //Configure stuff
    }
    
    @IBAction func addMonthButtonTapped(_ sender: UIButton) {
        selectedDate = Calendar.current.date(byAdding: .month,value: 1,to: selectedDate)!
        
        //Tell the ExampleCell class to add a month to its selectedDate property.
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "dateUpdated"),object: nil)
    }
    }
class ExampleCell: UICollectionViewCell {
        
        var selectedDate = Date()
        
        func configureCell(with indexPath: IndexPath) {
            //Configure stuff
        }
        
        
}

我是否使用委托/协议方法获取数据?

protocol DateSwitcherDelegate {
    func switchDate(with date: Date)
}

class ExampleHeader: UICollectionReusableView {

    var dateSwitcherDelegate: DateSwitcherDelegate!

@IBAction func leftButtonpressed(_ sender: UIButton) {
        selectedDate = Calendar.current.date(byAdding: .month,value: -1,to: selectedDate)!

        dateSwitcherDelegate.switchDate(with: selectedDate)

        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "dateUpdated"),object: nil)
    }
}
extension ExampleCell: DateSwitcherDelegate {
    
    func switchDate(with date: Date) {
        selectedDate = date
    }
    
}

我尝试了此操作,但收到了: “致命错误:在隐式展开可选值:文件时意外发现nil”

我能正确处理吗?还是应该使用ExampleViewController类传递数据?

我是编程新手,这是我的第一个项目。

提前感谢您的帮助!

编辑:如@VadimBelyaev所建议,我正在为日期创建模型。这就是我到目前为止所拥有的。

import Foundation

class MonthToAdjust {
    
    var date = Date()
    
    init(date: Date) {
        self.date = date
    }
    
    func increaseDateByAMonth() {
        date = Calendar.current.date(byAdding: .month,to: date)!
    }
    func decreaseDateByAMonth() {
        date = Calendar.current.date(byAdding: .month,to: date)!
    }
    
}

如何在ViewController中访问变量?如何更新模型中的日期?谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)