gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:35:28 27 4
gpt4 key购买 nike

我正在尝试在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com