UICollectionView委托方法存在问题

时间:2018-11-30 16:56:30

标签: ios swift uicollectionview uikit

我正在尝试使用UICollectionView,它正在视图中显示,但是未调用委托方法。这是我要尝试的方法:

在情节提要上,我有一个UITableViewController,其中有一个TableView和一个UIView,如下所示:

enter image description here

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

class FeedTableViewController: UITableViewController {

    @IBOutlet weak var bannerView: UIView!

}

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

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实例化并显示在视图中,正如我们在此处的青色框上看到的那样:

enter image description here

但是未调用委托方法numberOfItemsInSectioncellForItemAt。而且我已经将BannerViewController注册为UICollectionView的数据源和委托,所以我不知道为什么它不起作用。

1 个答案:

答案 0 :(得分:1)

您需要强烈引用(使其作为实例var)

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 {