【发布时间】:2021-08-19 09:33:04
【问题描述】:
我目前正在使用 UICollectionView 编写应用程序,仅当我启动应用程序时,CollectionView 不显示。在我的应用程序中,我不使用 StoryBoard,而只使用 ViewController。这就是为什么我在我的应用程序中从 UICollectionViewController 继承。但是当我启动应用程序时, UICollectionView 根本不显示。通过打印命令,我发现调用了 numberOfItemsInSection 方法,但没有调用 cellForItemAt indexPath。我也知道这可能是一个双重问题,但我仍然没有找到解决方案 2 小时。 CollectionView.delegate 和 collectionView.dataSource 都设置为 = self 并且约束实际上没有问题,因为例如我可以将 UICollectionView 的背景变为黑色,并且所有内容都在那里正确显示。 我希望有一个人可以帮助我。 最好的问候
// MARK: - Collectionview
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("Item wurde erstellt")
return messages.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ChatMessageCollectionViewCell
let message = messages[indexPath.item]
cell.textView.text = message.message
setUpCell(cell, message)
cell.bubbleViewWidthAnchor?.constant = estimateFrameForText(text: message.message!).width + 32
print("Celle wird erstellt")
return cell
}
【问题讨论】:
-
你检查你继承
UICollectionViewDataSource吗? -
嗨。谢谢您的回答!不,我不继承 UICollectionViewDataSource。但即使我尝试继承它,我也会收到错误:
Redundant conformance of 'ChatController' to protocol 'UICollectionViewDataSource'
标签: swift xcode uicollectionview uicollectionviewcell