ios - CollectionViewCell 中 CollectionView 的数据源始终为 nil
问题描述
我有一个UICollectionView
里面的一个UICollectionViewCell
,和一个单独的NSObject
那个是dataSource
。我可以dataSource
为外部设置UICollectionView
,但不能为内部设置。
这是包含 internal 的单元格UICollectionView
:
class FeaturedCell: UICollectionViewCell, UICollectionViewDelegate {
@IBOutlet var collectionView: UICollectionView!
let data = FeaturedData()
override init(frame: CGRect) {
super.init(frame: frame)
//setUp()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
func setUp() {
collectionView.dataSource = data
collectionView.delegate = self
collectionView.reloadData()
}
}
extension FeaturedCell {
func setCollectionViewDataSourceDelegate <D: FeaturedData> (_ dataSourceDelegate: D, forRow row: Int) {
collectionView.delegate = dataSourceDelegate
collectionView.dataSource = dataSourceDelegate
collectionView.reloadData()
print("Reload Data")
}
}
并且UIView
包含外部UICollectionView
:
class MainView: UIView, UICollectionViewDelegate {
@IBOutlet var collectionView: UICollectionView!
let data = MainData()
override func awakeFromNib() {
setUp()
}
func setUp() {
collectionView.dataSource = data
collectionView.delegate = self
collectionView.backgroundColor = UIColor.orange
collectionView.collectionViewLayout = createLayout()
collectionView.isPagingEnabled = true
collectionView.bounces = false
collectionView.allowsSelection = true
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
print("WillDisplay")
guard let cell: FeaturedCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeaturedCell", for: indexPath) as? FeaturedCell else {
fatalError("Unable to dequeue FeaturedCell.")
}
cell.setCollectionViewDataSourceDelegate(featuredData, forRow: indexPath.item)
}
}
这两种方法都被调用了,但从未设置dataSource
and 。delegates
我也完全按照本教程进行操作(使用UITableView
, 甚至),但它仍然不会设置dataSource
or delegate
。我究竟做错了什么?
解决方案
我认为这会有所帮助
class FeaturedCell: UICollectionViewCell {
@IBOutlet var collectionView: UICollectionView!
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}
extension FeaturedCell {
func setCollectionViewDataSourceDelegate <T: UICollectionViewDelegate , D: FeaturedData> (delegate: T dataSource: D, forRow row: Int) {
collectionView.delegate = delegate
collectionView.dataSource = dataSource
collectionView.reloadData()
print("Reload Data")
}
}
在主视图中
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
print("WillDisplay")
guard let cell: FeaturedCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeaturedCell", for: indexPath) as? FeaturedCell else {
fatalError("Unable to dequeue FeaturedCell.")
}
cell.setCollectionViewDataSourceDelegate(self, featuredData, forRow: indexPath.item)
}
对于本教程,您遵循此教程的Github 链接,将您的代码与该链接进行比较,看看您错过了哪里。
希望这会有所帮助。
推荐阅读
- xamarin.ios - 在 Xamarin 中如何等效于 UIImage.withTintColor?
- flutter - 当我从 firebase 读取数据时,它会显示我的数据。但它也显示所有用户数据。不仅是我的数据。但我只想要我的数据。不是其他用户
- html - 如何在 vuejs 中加载 html 模板
- java - 使用 Java 在外部显示器上播放视频
- java - 如何将我的 jsp 变量传递给我的标签?(HTML)
- javascript - 在 url 中并通过前端传递值并在 angularjs 中捕获后端
- node.js - 如何制作邀请命令(Discord.js)
- visual-studio-2017 - 每当我尝试安装某个 Discord 包时,就会弹出依赖注入错误
- apache-kafka - Linux生产者到Windows消费者未能解决
- prolog - SWI Prolog 为列表列表中的所有变量提供特定值