![](/img/trans.png)
[英]iOS UICollectionView with horizontal scrolling inside vertical UICollectionView cells reusing
[英]Cells disappearing with horizontal scrolling in UICollectionView
我在这个奇怪的问题上停留了一段时间,似乎无法解决。
设定
UICollectionView
UICollectionViewCell
C是当用户在B中轻UICollectionView
添加为B的子视图的UICollectionView
+------------------------------+ |A | | | | +----------+ +----------+ | | |B | |B | | | |----------| | | | | | | | | | | |C | | | | | +----------+ +----------+ | +------------------------------+
问题
将C的UICollectionViewFlowLayout
滚动方向属性初始化为.horizontal
, 并滚动经过第二个单元格时 ,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我在这里尝试过尼克的答案。
当滚动方向设置为.vertical
,一切正常。
设置为.horizontal
C时不显示任何单元格...
傻子,傻子菜鸟的错误。 如果有人像我一样需要眼镜,就把它留在这里:)
集合视图没有水平约束...
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
修复它(显然)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.