gpt4 book ai didi

ios - UICollectionView 中的单元格随着水平滚动而消失

转载 作者:行者123 更新时间:2023-11-30 12:26:51 24 4
gpt4 key购买 nike

我已经被这个奇怪的问题困扰了一段时间,似乎无法弄清楚。

设置

  • A 是一个 UICollectionView
  • B是 A 的 UICollectionViewCell
  • C 是当用户点击 B 时作为 B 的 subview 添加的 UICollectionView

    +------------------------------+
    |A |
    | |
    | +----------+ +----------+ |
    | |B | |B | |
    | |----------| | | |
    | | | | | |
    | |C | | | |
    | +----------+ +----------+ |
    +------------------------------+

问题

当将 C 的 UICollectionViewFlowLayout 滚动方向属性初始化为 .horizo​​ntal 时,并滚动经过第二个单元格,C 的细胞正在消失。

作为奖励,C 本身将从 UI 中消失:用户可以再次点击该单元格,该单元格将执行 C 常规删除时执行的所有操作。当再次点击以正常重新显示它时,会触发 C 显示时触发的操作,但在视觉上仍然找不到 C。

当滚动方向设置为 .vertical 时,不会出现此问题。

有人遇到过类似的问题或有任何关于这里发生的事情的线索吗?

提前致谢。

<小时/>

编辑实际实现

(B)

import UIKit

class CollectionViewCell: UICollectionViewCell {

//...

private let cellId = "cellId"

private lazy var label: UILabel = {

return CollectionViewCell._label()
}()

fileprivate lazy var gallery: UICollectionView = {

return CollectionViewCell._gallery()
}()

//...

override init(frame: CGRect) {
super.init(frame: frame)

isOpaque = true
clipsToBounds = true
layer.borderWidth = 1.5
layer.cornerRadius = 8

// ...
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func onSwitchDisplayGallery(_ isDiplaying: Bool) {

switch isDiplaying {

case true:

addSubview(label)
label.topAnchor.constraint(equalTo: topAnchor).isActive = true
label.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
label.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
label.heightAnchor.constraint(equalToConstant: frame.height / 5.25).isActive = true

gallery.register(NestedCollectionViewCell.self, forCellWithReuseIdentifier: cellId)
gallery.delegate = self
addSubview(gallery)
gallery.topAnchor.constraint(equalTo: label.bottomAnchor).isActive = true
gallery.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
gallery.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
gallery.heightAnchor.constraint(equalTo: heightAnchor, constant: -frame.height / 5.25).isActive = true

case false:

print("removed cv")
// ...
}
}

//...
}

extension CollectionViewCell: UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

return gallery.frame.size
}
}


private extension CollectionViewCell {

class func _gallery() -> UICollectionView {

let layout = UICollectionViewFlowLayout()

layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.scrollDirection = .horizontal

let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)

cv.isPagingEnabled = true
cv.showsHorizontalScrollIndicator = false
cv.translatesAutoresizingMaskIntoConstraints = false
return cv
}

class func _label() -> UILabel {

let label = UILabel()

label.font = UIFont(name: "Montserrat-Regular", size: 15)
label.textColor = .white
label.translatesAutoresizingMaskIntoConstraints = false
return label
}
}

(C)

import UIKit

class NestedCollectionViewCell: UICollectionViewCell {

private let containerView = NestedCollectionViewCell._containerView()

override init(frame: CGRect) {
super.init(frame: frame)

isOpaque = true
clipsToBounds = true
backgroundColor = .white

addSubview(containerView)
containerView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
containerView.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
containerView.heightAnchor.constraint(equalTo: heightAnchor).isActive = true

//...
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

private extension NestedCollectionViewCell {

class func _containerView() -> UIImageView {

let view = UIImageView()

view.contentMode = .scaleAspectFill
view.translatesAutoresizingMaskIntoConstraints = false
return view
}
}
<小时/>

编辑2我已经尝试过尼克的回答here .

当滚动方向设置为.vertical时,一切正常。

当设置为.horizo​​ntal时,C不显示单元格...

最佳答案

愚蠢、愚蠢的菜鸟错误。如果其他人像我一样需要眼镜,请将其留在这里:)

Collection View 没有水平约束...

addSubview(gallery)
gallery.topAnchor.constraint(equalTo: label.bottomAnchor).isActive = true
gallery.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

将其更正为

addSubview(gallery)
gallery.topAnchor.constraint(equalTo: label.bottomAnchor).isActive = true
gallery.leftAnchor.constraint(equalTo: leftAnchor).isActive = true

修复了它(显然)。

关于ios - UICollectionView 中的单元格随着水平滚动而消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44079861/

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