首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UICollectionViewController单元格在点击或滚动时消失

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

Stack Overflow用户
提问于 2019-07-18 00:44:33
回答 1查看 246关注 0票数 0

我正在尝试在tabBarController上添加CollectionView

对于网格collectionView

所以我做了一个UICollectionViewController作为另一个快速文件

我作为UICollectionViewController.view添加到ViewController(导航控制器)

但是当我点击collectionView的时候,细胞立刻消失了。

我想我确实跟着导游找了两天..但我不能修好它。

有些人说这是添加collectionView作为子视图的问题,但我不明白

这是自定义UICollectionView,MyFeedController.swift

代码语言:javascript
运行
复制
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很简单

代码语言:javascript
运行
复制
//
//  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)
    }
}

所以我在MyPageController中添加了MyFeedController (这是一个UIViewController)

代码语言:javascript
运行
复制
    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
    }
EN

回答 1

Stack Overflow用户

发布于 2019-07-18 02:56:36

我不知道你在哪里设置集合视图的dataSource。在这一行设置一个断点,以确保它被调用:

代码语言:javascript
运行
复制
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MyFeedCell
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57080711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档