【问题标题】:collectionView is not showing in UITableViewController集合视图未显示在 UITableViewController 中
【发布时间】:2021-10-03 17:00:39
【问题描述】:

我想在 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

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

【问题讨论】:

    标签: ios swift uitableview uicollectionview swift5


    【解决方案1】:

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

    【讨论】:

    • 我不能......我尝试在我的 configureCollectionView() 函数和 viewDidLoad() 中调用它。我也试过 tableView.isHidden = true。那只会让屏幕变黑,检查我现在是否看到了 collectionView,我设置了 collectionView.backgroundColor = .red,仍然没有显示。所以显然我没有在正确的地方调用collectionView?
    • 我正在使用搜索栏。我希望tableView在我使用searchBar时出现,如果我不使用它,我希望collectionView出现。
    • layout 的大小设置了吗?
    • 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)!)
    • @swiftcrackhead1 我认为根据您的要求,您可以使用带有表格和集合视图的 UIViewcontroller。这将是一个简单的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 2020-01-20
    • 1970-01-01
    • 2018-04-02
    相关资源
    最近更新 更多