1

我想在我的 Header 中有一个类似于 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
    }


}```
4

2 回答 2

0

很好,我想通了。我将库中的集合视图添加到情节提要的标题中。然后我将它连接为我的 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
    }


}
于 2019-05-27T18:48:46.683 回答
0

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

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

}

在这里,您将添加自定义视图UICollectionViewasUICollectionReusableView

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

于 2019-05-27T10:17:20.493 回答