【问题标题】:Why won't my swift collection view show anything为什么我的快速收藏视图不显示任何内容
【发布时间】:2021-07-04 04:43:42
【问题描述】:

我想做一个简单的集合视图,只显示一些图片,但单元格不会显示

import UIKit

final class HomePageViewController: UICollectionViewController {
    // MARK: - Properties

    
    override func loadView() {
        super.loadView()
        
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(collectionView)
        NSLayoutConstraint.activate([
            collectionView.topAnchor.constraint(equalTo: self.view.topAnchor),
            collectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
            collectionView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
            collectionView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
            ])
        self.collectionView = collectionView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView.backgroundColor = .white
        self.collectionView.dataSource = self
        self.collectionView.delegate = self

        
        self.collectionView.register(HomePageCell.self, forCellWithReuseIdentifier: "HomePageCell")
    }

    
    
    // MARK: - Private


    
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    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: "HomePageCell", for: indexPath) as! HomePageCell
            cell.Picture.image = UIImage(named: "songpic-1")
        
        return cell
    }

}

extension HomePageViewController: UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        return CGSize(width: collectionView.bounds.size.width - 16, height: 200)
    }
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 8
    }
    
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.init(top: 8, left: 8, bottom: 8, right: 8)
    }
}

import UIKit



class HomePageCell: UICollectionViewCell {
    
    
    @IBOutlet weak var Picture: UIImageView!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let Picture = UIImageView(frame: .zero)
        Picture.translatesAutoresizingMaskIntoConstraints = false
        self.contentView.addSubview(Picture)
        NSLayoutConstraint.activate([
            Picture.topAnchor.constraint(equalTo: self.contentView.topAnchor),
            Picture.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor),
            Picture.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor),
            Picture.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor),
            ])
        
        
        self.contentView.backgroundColor = .lightGray
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        fatalError("Interface Builder is not supported!")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
    
    }
    
        
    
}

【问题讨论】:

    标签: swift uicollectionview


    【解决方案1】:

    您可以删除/评论您的loadView() 实现,一切都应该可以正常工作。您仍然可以使用 UICollectionView

    loadView() 实现应该提供一个UIView 实例,该实例将分配给UIViewController.view 属性。然而,在您的实现中,我们直接通过self.view 使用它,而不是先分配它。

    来自UIViewController.loadView() docs -

    您可以覆盖此方法以手动创建视图。 如果您选择这样做,请将视图层次结构的根视图分配给view 属性。您创建的视图应该是唯一的实例,并且不应与任何其他视图控制器对象共享。 此方法的自定义实现不应调用 super。


    如果仍然没有出现怎么办?

    使用View Hierarchy Debugger 检查在运行时您的collectionView 具有非零widthheight 值。如果width OR height 中的任何一个为零,您将遇到此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-15
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多