尝试将 CollectionView 添加到 ResuableView 标头,如 Instagram 或 SNapcha



我想在我的标题中有一个类似于Instagram和Snapchat上的CollectionView,用于我的主要CollectionView,这将是提要。当我尝试从库中添加集合视图并在实现委托方法后将标题设置为委托和数据源时,我仍然在标题中看不到任何内容。

    //let layout = UICollectionViewFlowLayout()
    let headerCollection = UICollectionView(frame: CGRect(x: 0, y: 0, width: 50, height: 90), collectionViewLayout: UICollectionViewFlowLayout())
    override func awakeFromNib() {
        super.awakeFromNib()
//       headerCollection.delegate = self
//        headerCollection.dataSource = self
    }

}
extension HeaderforFeed: UICollectionViewDataSource, UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return CollectionViewData.Dict_StrImg.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = UICollectionViewCell(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        cell.backgroundColor = .red
        return cell
    }

}```

根据您的代码,我认为问题是您没有将其添加到标题中(我将其理解为UICollectionView中的标题部分(。如果是这样,请尝试将其添加到

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
}

在这里,您将添加带有UICollectionView的自定义视图作为UICollectionReusableView

我不知道这是否可以帮助你,但根据你的问题,我理解的是

很好,

我想通了。我将库中的集合视图添加到情节提要中的标题中。然后我把它作为我的UICollectionReusableView类中的一个出口连接起来。然后,我使该类为集合视图实现委托方法,但为标头中的委托方法实现。

使可重用视图成为委托和数据源,它对我有用

//  HeaderforFeed.swift
//  finishedFeednStories
//
//  Created by Guh Suck Yu Mada
//  Copyright © 2019 Bumboklat. All rights reserved.
//
import UIKit
class HeaderforFeed: UICollectionReusableView {
    //let layout = UICollectionViewFlowLayout()

    @IBOutlet weak var storiesCollection: UICollectionView!

    override func awakeFromNib() {
        super.awakeFromNib()
       storiesCollection.delegate = self
        storiesCollection.dataSource = self
    }

}
extension HeaderforFeed: UICollectionViewDataSource, UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return CollectionViewData.Dict_StrImg.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "storiesCell", for: indexPath) as! StoriesCell
        cell.storiesImg.image = Array(CollectionViewData.Dict_StrImg)[indexPath.row].value
        cell.storiesLabel.text = Array(CollectionViewData.Dict_StrImg)[indexPath.row].key
        return cell
    }

}

最新更新