ios - cellForItemAt 未在 collectionView 中调用

标签 ios swift uicollectionview autolayout collectionview

我有一个 inputView,其中将显示一个 collectionView。我为那个 inputView 使用了自动布局。运行应用程序时,会调用 numberOfItemsInSection 但不会调用 cellForItemAt。所以没有显示单元格。我使用约束做错了什么吗? 我还创建了项目 https://drive.google.com/file/d/0B5UHWsK1E6dSRDA5bmtSY2ZZbGs/view

ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()
    textField.inputView = InputPhotoView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 265))
    textField.becomeFirstResponder()
}

InputPhotoView.swift

class InputPhotoView: UIView {
let CellIdentifier = "Cell"
override init(frame: CGRect) {
    super.init(frame: frame)
    collectionView.dataSource = self
    collectionView.register(CustomCell.self, forCellWithReuseIdentifier: CellIdentifier)
    setupViews()
}
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    fatalError("Not yet implented")
}
func setupViews() {
    self.backgroundColor = UIColor.brown
    self.addSubview(collectionView)
    setupConstraints()
}
func setupConstraints() {
    NSLayoutConstraint.activate([
        collectionView.leadingAnchor.constraint(equalTo: self.trailingAnchor, constant: 50),
        collectionView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0),
        collectionView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0),
        collectionView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0)
        ])
}
public lazy var collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    layout.itemSize = CGSize(width: 100, height: 100)
    layout.minimumInteritemSpacing = 0
    layout.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
    let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
    collectionView.translatesAutoresizingMaskIntoConstraints = false
    collectionView.contentInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
    return collectionView
}()

} 数据源

extension InputPhotoView : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    print("number of items")
    return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellIdentifier, for: indexPath) as! CustomCell
    return cell
}

最佳答案

您的代码存在约束冲突,因此 collectionView 不可见。尝试更改此行:

collectionView.leadingAnchor.constraint(equalTo: self.trailingAnchor, constant: 50)

像这样:

collectionView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 50)

关于ios - cellForItemAt 未在 collectionView 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46853189/

相关文章:

ios - 如何禁用与 UIBarButtonItem 的用户交互?

iOS - UIImage 调整到更大变得模糊

ios - Swift - iOS - 不同格式的日期和时间

swift - Xcode 7.3 已弃用 "++"和 "--"运算符

ios - 当我单击文本字段时,两个都实现了类 _PointQueue ...我该如何解决这个问题?

ios - 调整 UICollectionView 大小

ios - 如何使 UICollectionView 通过自动调整大小来填充其 super View ?

ios - startMonitoringForRegion 与 CLRegion :containCoordinate

ios - UIView 背景色比 UICollectionView 背景色深

swift - 在 UICollectionViewCompositionalLayout/UICollectionViewDiffableDataSource 的两个不同部分显示相同的对象