swift 如何修复UICollectionViewCells有时无法加载?

yhuiod9q  于 2023-06-04  发布在  Swift
关注(0)|答案(1)|浏览(125)

我试图在我的应用程序的顶部做一个小的水平UICollectionView,但每次我加载应用程序时,总是有2或3个单元格没有加载其内容。没有加载的细胞也在变化。
以下是我所做的:

public var collectionViewDates = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(collectionViewDates)
    collectionViewDates.frame = CGRect(x: 0,
                                       y: 90,
                                       width: view.width,
                                       height: 50)
}

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 30
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DatesCollectionViewCell.identifier, for: indexPath) as! DatesCollectionViewCell
         cell.dateLabel.text = "I dag\(indexPath.row)"
         return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DatesCollectionViewCell.identifier, for: indexPath) as! DatesCollectionViewCell
        return CGSizeMake(cell.dateLabel.width + 30, collectionView.height)
        
    }

在细胞中:

public var dateLabel: UILabel = {
        let label = UILabel()
        label.text = "I dag"
        label.adjustsFontSizeToFitWidth = true
        label.font = .systemFont(ofSize: 20, weight: .bold)
        label.backgroundColor = .systemBlue
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(dateLabel)
        
        dateLabel.frame = CGRect(x: 0,
                                 y: 0,
                                 width: 50,
                                 height: contentView.height)
        
        dateLabel.center.y = contentView.center.y
        dateLabel.center.x = contentView.center.x
        
        
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

我不明白为什么单元格有时会加载,因为我以前用其他水平UICollectionViews做过。但这一次我没有那么幸运。
以下是问题的图像:

ioekq8ef

ioekq8ef1#

首先,花一点时间学习自动布局。
其次,如果您想要50x50单元格,请在集合视图的流布局上设置.itemSize
看看这个:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    
    public var collectionViewDates: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let fl = UICollectionViewFlowLayout()
        fl.scrollDirection = .horizontal
        fl.minimumLineSpacing = 24.0
        fl.minimumInteritemSpacing = 24.0
        fl.itemSize = .init(width: 50.0, height: 50.0)
        
        collectionViewDates = UICollectionView(frame: .zero, collectionViewLayout: fl)
        
        collectionViewDates.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(collectionViewDates)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            collectionViewDates.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
            collectionViewDates.leadingAnchor.constraint(equalTo: g.leadingAnchor),
            collectionViewDates.trailingAnchor.constraint(equalTo: g.trailingAnchor),
            collectionViewDates.heightAnchor.constraint(equalToConstant: 64.0),
        ])
        
        collectionViewDates.register(DatesCollectionViewCell.self, forCellWithReuseIdentifier: DatesCollectionViewCell.identifier)
        collectionViewDates.dataSource = self
        collectionViewDates.delegate = self
        
        collectionViewDates.backgroundColor = .black
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 30
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DatesCollectionViewCell.identifier, for: indexPath) as! DatesCollectionViewCell
        cell.dateLabel.text = "I dag\(indexPath.row)"
        return cell
        
    }
    
}

class DatesCollectionViewCell: UICollectionViewCell {
    static let identifier: String = "datesCell"
    
    public var dateLabel: UILabel = {
        let label = UILabel()
        label.text = "I dag"
        label.adjustsFontSizeToFitWidth = true
        label.font = .systemFont(ofSize: 20, weight: .bold)
        label.backgroundColor = .systemBlue
        label.textColor = .white
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    
    private func commonInit() {

        dateLabel.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(dateLabel)
        
        NSLayoutConstraint.activate([
            dateLabel.topAnchor.constraint(equalTo: contentView.topAnchor),
            dateLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            dateLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
            dateLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
        ])

    }
}

结果:

编辑

如果希望单元格的大小适合标签的宽度...
为流布局指定 * 估计 * 项目大小:

//fl.itemSize = .init(width: 50.0, height: 50.0)
fl.estimatedItemSize = .init(width: 50.0, height: 50.0)

并且不要将标签设置为自动缩小字体:

//label.adjustsFontSizeToFitWidth = true

相关问题