【发布时间】:2020-02-24 00:31:50
【问题描述】:
我在UICollectionViewCell 中有一个UICollectionView,还有一个单独的NSObject,即dataSource。我可以为外部UICollectionView 设置dataSource,但不能为内部设置。
这是包含内部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")
}
}
还有包含外部UICollectionView的UIView:
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 和delegates 从未被设置。我也完全遵循this 教程(甚至使用UITableView),但它仍然不会设置dataSource 或delegate。我究竟做错了什么?
【问题讨论】:
标签: ios swift xcode uitableview uicollectionview