gpt4 book ai didi

ios - UICollectionView 委托(delegate)方法的问题

转载 作者:行者123 更新时间:2023-11-28 14:00:58 25 4
gpt4 key购买 nike

我正在尝试使用 UICollectionView,它显示在 View 上,但未调用委托(delegate)方法。这是我正在尝试做的事情:

在 Storyboard 上,我有一个 UITableViewController,它有一个 TableView 和一个 UIView,如下所示:

enter image description here

我在我的 UITableViewController 类上为 UIView 创建了一个导出:

class FeedTableViewController: UITableViewController {

@IBOutlet weak var bannerView: UIView!

}

viewDidLoad() 函数上,我正在实例化 UIViewController 类,它将成为我的 UICollectionView 的委托(delegate)和数据源:

override func viewDidLoad() {
let bannerViewController = BannerViewController()
bannerView.addSubview(bannerViewController.view)

bannerViewController.view.translatesAutoresizingMaskIntoConstraints = false
bannerViewController.view.leadingAnchor.constraint(equalTo: bannerView.leadingAnchor).isActive = true
bannerViewController.view.trailingAnchor.constraint(equalTo: bannerView.trailingAnchor).isActive = true
bannerViewController.view.topAnchor.constraint(equalTo: bannerView.topAnchor).isActive = true
bannerViewController.view.bottomAnchor.constraint(equalTo: bannerView.bottomAnchor).isActive = true
}

下面是BannerViewController类的完整代码:

class BannerViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 125)
let collectionView = UICollectionView(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
collectionView.delegate = self
collectionView.dataSource = self
collectionView.backgroundColor = .cyan

view.addSubview(collectionView)
}

}

extension BannerViewController: UICollectionViewDataSource {

// MARK: UICollectionViewDataSource

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)
cell.backgroundColor = .red
return cell
}

}

extension BannerViewController: UICollectionViewDelegateFlowLayout {

// MARK: UICollectionViewDelegateFlowLayout

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.size.width, height: collectionView.bounds.size.height)
}

}

UICollectionView 正在被实例化并出现在 View 上,正如我们可以在此处的青色框中看到的那样:

enter image description here

但是委托(delegate)方法 numberOfItemsInSectioncellForItemAt 没有被调用。我已经将 BannerViewController 注册为 UICollectionView 的数据源和委托(delegate),所以我不知道为什么它不起作用。

最佳答案

您需要持有对(使其成为实例变量)的强引用

var bannerViewController:BannerViewController!

也适当添加吧

bannerViewController = BannerViewController()
addChildViewController(bannerViewController)
view.addSubview(bannerViewController.view)
bannerViewController.view.translatesAutoresizingMaskIntoConstraints =false
NSLayoutConstraint.activate([
bannerViewController.view.leadingAnchor.constraint(equalTo: bannerView.leadingAnchor),
bannerViewController.view.trailingAnchor.constraint(equalTo: bannerView.trailingAnchor),
bannerViewController.view.topAnchor.constraint(equalTo: bannerView.topAnchor),
bannerViewController.view.bottomAnchor.constraint(equalTo: bannerView.bottomAnchor)
])
bannerViewController.didMove(toParentViewController: self)

也别忘了

extension BannerViewController: UICollectionViewDelegate {

关于ios - UICollectionView 委托(delegate)方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53561808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com