ios - UICollectionViewController 单元格在点击或滚动时消失

标签 ios swift

我正在尝试在 tabBarController 上添加 CollectionView

对于网格集合View

所以我制作了一个 UICollectionViewController 作为另一个 swift 文件

我将 ViewController(导航 Controller )添加为 UICollectionViewController.view

但是当我点击 collectionView 时,单元格立即消失。 before-tap after-tap

我想我确实按照指南搜索了两天..但我无法修复它......

有人说这是添加collectionView作为 subview 的问题,但我不明白

这是自定义 UICollectionView,MyFeedController.swift

import UIKit

class MyFeedController: BaseListController, UICollectionViewDelegateFlowLayout {

    let cellId = "cellId"

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.backgroundColor = .purple

        collectionView.register(MyFeedCell.self, forCellWithReuseIdentifier: cellId)
        collectionView.delegate = self


        if let layout = collectionViewLayout as? UICollectionViewFlowLayout {
            layout.scrollDirection = .vertical
        }
    }

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

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MyFeedCell
        cell.backgroundColor = .red
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let screenSize: CGRect = UIScreen.main.bounds
        let screenWidth = screenSize.width
        return .init(width: (screenWidth - 4 - 4) / 3, height: (screenWidth - 4 - 4) / 3)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return .init(top: 2, left: 0, bottom: 2, right: 0)
    }
}

这是我的自定义单元格,MyFeedCell.swift,很简单

//
//  MyFeedCollectionController.swift
//  My-Lord-Cat
//
//  Created by Cory Kim on 17/07/2019.
//  Copyright © 2019 Cory Kim. All rights reserved.
//

import UIKit

class MyFeedController: BaseListController, UICollectionViewDelegateFlowLayout {

    let cellId = "cellId"

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.backgroundColor = .purple

        collectionView.register(MyFeedCell.self, forCellWithReuseIdentifier: cellId)
        collectionView.delegate = self


        if let layout = collectionViewLayout as? UICollectionViewFlowLayout {
            layout.scrollDirection = .vertical
        }
    }

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

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MyFeedCell
        cell.backgroundColor = .red
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let screenSize: CGRect = UIScreen.main.bounds
        let screenWidth = screenSize.width
        return .init(width: (screenWidth - 4 - 4) / 3, height: (screenWidth - 4 - 4) / 3)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return .init(top: 2, left: 0, bottom: 2, right: 0)
    }
}

所以我将 MyFeedController 添加到 MyPageController(这是一个 UIViewController)


    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white

        navigationController?.navigationBar.isHidden = true

        setupLayout()
        setupMyFeedGridCollectionView()
    }

    // other views are set on setupLayout()

    fileprivate func setupMyFeedGridCollectionView() {

        let myFeedController = MyFeedController()

        view.addSubview(myFeedController.view)
        myFeedController.view.anchor(top: infoStackView.bottomAnchor, leading: view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor, padding: .init(top: 10, left: 0, bottom: 0, right: 0))
    // anchor method is extension for autolayout
    }

最佳答案

我不明白你在哪里设置collectionView的数据源。在此行设置一个断点以确保它被调用:

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MyFeedCell

关于ios - UICollectionViewController 单元格在点击或滚动时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57080711/

相关文章:

ios - 如何在 Xcode 7.0.1 上模拟 WatchOS

iphone - 检查对象是否为 UIKeyboard

ios - applyImpulse on SKSpriteNode in touchesBegan 不工作

ios - 如何通过单击按钮以编程方式增加自定义 uiview 及其在 View Controller 中的所有 subview 的宽度

ios - UIFont 自定义字体未在设备上显示

ios - 异步服务调用的 TableView 单元测试

ios - 下载和播放离线 HLS 内容 - iOS 10

swift - 我如何快速从左向右移动图像?

ios - 在枚举中使用静态让而不是大小写

ios - 如何在 Swift 中模仿 safari 的搜索栏 UI/动画?