自定义集合视图单元在点击时消失

5

我正在开发一款iPad应用程序,该应用程序应该在屏幕上显示4个并列的CollectionView。CollectionView的高度应为屏幕高度的3/4,因此布局将类似于以下内容:

 ___________________
|    top content    |
|-------------------|
| CV | CV | CV | CV |
|____|____|____|____|

我试图将所有东西缩小,并创建一个新项目,仅包含其中一种集合视图,但我始终遇到同样的问题:当我点击其中一个单元格时,所有单元格都消失了。重现方法如下:
  • 使用Swift作为语言,创建模板为“Single View Application”的新项目
  • 设置Storyboard:
    • 在Storyboard中拖入一个新的CollectionView Controller
    • 将Storyboard ID设置为“CollectionViewController”
    • 对于单元格:将标识符设为MyCollectionViewCell,在单元格中拖动标签并设置约束
  • 创建具有以下源代码的文件:

CollectionViewCell.swift:(通过Ctrl-Drag标签到源代码来创建outlet)

import UIKit

class CollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var label: UILabel!

}

CollectionViewController.swift:(注意viewDidLoad实现中的注释)

import UIKit

private let reuseIdentifier = "MyCollectionViewCell"

class CollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        // this has to be removed to work with a custom cell class
        // self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

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

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell
        cell.label.text = "\(indexPath.section)-\(indexPath.row)"
        return cell
    }

}

更改ViewController.swift的实现:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let frame = view.frame
        let vc = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "CollectionViewController")
        vc.view.frame = CGRect(x: 0.0, y: frame.size.height * 0.25, width: frame.size.width, height: frame.size.height * 0.75)
        self.view.addSubview(vc.view)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

这是iOS模拟器的截图: https://youtu.be/VVBsTnYLGM4 我希望我没有忘记任何重要信息以复制这个问题。 以防万一,我把项目上传到了我的dropbox。 https://dl.dropboxusercontent.com/u/607872/CollectionViewBugZip.zip 请问有人能告诉我我做错了什么吗?

现在正在看它 ;) - CZ54
我刚刚注意到另一件事情:即使我在故事板中删除了集合视图与其数据源/代理之间的连接,单元格仍然会显示。因此,一定存在某些隐式的数据源和代理,但我无法弄清楚是如何/为什么这样的。 - Dennis
自动调整大小的单元格应该如何帮助我?我有什么遗漏吗? - Dennis
3个回答

19

原来的错误在于将集合视图添加为子视图,这被认为不是一个好的做法。显然,当仅添加子视图时,它会与其视图控制器“断开连接”。

这样做就可以解决问题: ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let frame = view.frame
    let vc = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "CollectionViewController")
    self.addChildViewController(vc) // <----------- !!!!
    vc.didMove(toParentViewController: self) // <-- !!!!
    vc.view.frame = CGRect(x: 0.0, y: frame.size.height * 0.25, width: frame.size.width, height: frame.size.height * 0.75)
    self.view.addSubview(vc.view)
}

请解释它是如何工作的? - Saranjith

0

检查您的ViewController是否未被释放。编写deinit { print("Deinited") }并在那里设置断点。在我的情况下,CollectionView消失了,因为它与持有它的viewController一起被释放了。


0

Swift 5 / iOS 16

在我以编程方式实例化承载UICollectionView的UIViewController的位置下方添加以下两行代码对我有用。

我不知道为什么它有效,但是我已经彻底没有了想法,在Reddit上找到了这个解决方法,在我流汗并尝试我能想到的一切几个小时后,问题得到了解决。

    self.addChild(vcThatHostsCollectionView)
    vcThatHostsCollectionView(toParent: self)

这是我“消费者”视图控制器中的代码,用于实例化包含上述两行的集合视图托管视图控制器。
func configurePagerTabs() {
    let rect = CGRectMake(0, 0, UIScreen.main.bounds.width, 35.0)
    let pagerTabsViewController = PagerTabsViewController(frame: rect, tabTitles: tabTitles)
    self.addChild(pagerTabsViewController)
    pagerTabsViewController.didMove(toParent: self)
    pagerTabsViewController.delegate = self
    pagerTabsView = pagerTabsViewController.view
    view.addSubview(pagerTabsView)
}

这是视图控制器的一部分,用于创建集合视图,以展示它如何初始化事物。
class PagerTabsViewController: UIViewController {

    enum Section { case main }

    var dataSource                  : UICollectionViewDiffableDataSource<Section, String>! = nil
    var collectionView              : UICollectionView!     = nil
    var tabTitles                   : [String]!             = nil
    var delegate                    : PagerTabsDelegate?    = nil
    var reuseId                     = "TabCellReuse"
    var frame                       = CGRect.zero

    required init(coder: NSCoder) { fatalError("Not implemented to use with storyboard") }

    @objc init(frame: CGRect, tabTitles: [String]) {
        super.init(nibName: nil, bundle: nil)
        self.tabTitles = tabTitles
        self.frame = frame  
    }

    override func loadView() {
        collectionView = UICollectionView(frame: frame, collectionViewLayout: createLayout())
        collectionView.isPagingEnabled  = true              
        collectionView.delegate         = self
        collectionView.dataSource       = self
        collectionView.backgroundColor  = .blue
        registerCells()
        view = collectionView
    }

    .
    .
    .
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接