我正在尝试使用UICollectionView
,它正在视图中显示,但是未调用委托方法。这是我要尝试的方法:
在情节提要上,我有一个UITableViewController
,其中有一个TableView
和一个UIView
,如下所示:
我在我的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
实例化并显示在视图中,正如我们在此处的青色框上看到的那样:
但是未调用委托方法numberOfItemsInSection
和cellForItemAt
。而且我已经将BannerViewController
注册为UICollectionView
的数据源和委托,所以我不知道为什么它不起作用。
答案 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 {