首页 > 解决方案 > 集合视图未显示在 UITableViewController 中

问题描述

我想在 UITableViewController 类中显示 UICollectionView。我已经看过该工作的视频和代码,但显然它不再在 Swift 5 中工作,或者至少我不知道如何在 Swift 5 中设置它。我以编程方式完成所有事情。这是我在 viewDidLoad() 中调用的函数,但它似乎不起作用:

func configureCollectionView() {
    
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical

    let frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height - (tabBarController?.tabBar.frame.height)! - (navigationController?.navigationBar.frame.height)!)

    collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.alwaysBounceVertical = true
    collectionView.backgroundColor = .white
    collectionView.register(SearchCell.self, forCellWithReuseIdentifier: "SearchCell")

    tableView.addSubview(collectionView)
    tableView.separatorColor = .clear

}

我还调用了这些 collectionView 函数:

minimumInteritemSpacingForSectionAt

minimumLineSpacingForSectionAt

sizeForItemAt

numberOfItemsInSection

cellForItemAt

didSelectItemAt

我能做些什么来解决我的问题?

标签: iosswiftuitableviewuicollectionviewswift5

解决方案


设置成这样 collectionView.backgroundColor = .black,你能看到black背景吗?


推荐阅读