簡體   English   中英

單元格隨UICollectionView中的水平滾動消失

[英]Cells disappearing with horizontal scrolling in UICollectionView

我在這個奇怪的問題上停留了一段時間,似乎無法解決。

設定

  • A是一個UICollectionView
  • B是A的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM