简体   繁体   English

cellForItemAt没有在collectionView中调用

[英]cellForItemAt is not calling in collectionView

I have a inputView where a collectionView will be displayed. 我有一个inputView,其中将显示collectionView。 I used auto layout for that inputView. 我为该inputView使用了自动布局。 When running the app numberOfItemsInSection is called but cellForItemAt is not called. 运行应用程序时,将调用numberOfItemsInSection,但不调用cellForItemAt。 So no cell is displaying. 因此没有显示任何单元格。 Did I do anything wrong using constraint? 使用约束我做错了什么吗? I've also created project https://drive.google.com/file/d/0B5UHWsK1E6dSRDA5bmtSY2ZZbGs/view 我还创建了项目https://drive.google.com/file/d/0B5UHWsK1E6dSRDA5bmtSY2ZZbGs/view

ViewController.swift 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 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
}()

} DataSource } 数据源

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
}

} }

Your code has constraint conflicts so the collectionView is not visible. 您的代码具有约束冲突,因此collectionView不可见。 Try changing this line: 尝试更改此行:

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

To something like this: 对于这样的事情:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift DRY collectionView(_ cellForItemAt)调用 - Swift DRY collectionView(_ cellForItemAt) call 在Swift collectionView中仅调用一次cellForItemAt - cellForItemAt called only once in Swift collectionView 如何在collectionView单元格的cellForItemAt中为子视图着色? - How to color SubViews in a collectionView Cell's cellForItemAt? 未调用UICollectionView方法(collectionView cellForItemAt indexPath) - UICollectionView method (collectionView cellForItemAt indexPath ) not called 将数据从locationmanager(.. didrangbeacons)传递到collectionview(…cellforitemat) - Pass data from locationmanager(..didrangbeacons) to collectionview(…cellforitemat) CollectionView 方法 cellForItemAt indexPath:未被调用(Swift) - CollectionView method cellForItemAt indexPath: not being called (Swift) CollectionView cellForItemAt IndexPath中的SIGTRAP(TRAP_BRKPT)崩溃 - SIGTRAP(TRAP_BRKPT) Crash in CollectionView cellForItemAt IndexPath CollectionView cellForItemAt indexPath可以使单元出队,但单元的视图为nil - CollectionView cellForItemAt indexPath does deque cells but cell's view is nil 在cellForItemAt集合视图中不通过Swift 3中的collectionView.reloadData()更新indexPath - In cellForItemAt collection view is not updating indexPath by collectionView.reloadData() in swift 3 我想要表部分标题索引或collectionView(_:cellForItemAt :)中的标题标题 - I want the table section header index or section header title inside collectionView(_:cellForItemAt:)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM