Ios 从泛型类继承时从未调用UICollectionView委托方法

Ios 从泛型类继承时从未调用UICollectionView委托方法,ios,swift,xcode,uicollectionview,Ios,Swift,Xcode,Uicollectionview,我有一个名为GenericView的泛型类,它符合UICollectionViewDelegateFlowLayout协议,并持有collectionview。然后我有一个NewView类继承自GenericView 问题是,在NewView类中实现的委托方法如果在GenericView中不存在,就永远不会被调用。UIScrollView委托方法也不会被调用,除非它们有@objc前缀。但是,@objc前缀不能解决UICollectionView委托问题 简单代码: //标记:-泛型类 类Gene

我有一个名为
GenericView
的泛型类,它符合
UICollectionViewDelegateFlowLayout
协议,并持有collectionview。然后我有一个
NewView
类继承自
GenericView

问题是,在
NewView
类中实现的委托方法如果在
GenericView
中不存在,就永远不会被调用。UIScrollView委托方法也不会被调用,除非它们有
@objc
前缀。但是,
@objc
前缀不能解决UICollectionView委托问题

简单代码:

//标记:-泛型类
类GenericView:UIView、UICollectionViewDataSource、UICollectionViewDelegateFlowLayout{
关键:关键?
var collectionView=UICollectionView(帧:.0,collectionViewLayout:UICollectionViewFlowLayout())
重写初始化(帧:CGRect){
super.init(frame:frame)
//约束集合视图。
collectionView.TranslatesAutoResizezingMaskintoConstraints=false
addSubview(collectionView)
NSLayoutConstraint.activate([
collectionView.leadingAnchor.constraint(相等于:leadingAnchor),
collectionView.topAnchor.constraint(等式:topAnchor),
collectionView.trailingAnchor.constraint(等式:trailingAnchor),
collectionView.bottomAnchor.constraint(等号:bottomAnchor)
])
//设置委托和数据源。
collectionView.delegate=self
collectionView.dataSource=self
collectionView.register(UICollectionViewCell.self,forCellWithReuseIdentifier:“单元格”)
}
必需初始化?(编码器:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回100
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
let cell=collectionView.dequeueReusableCell(withReuseIdentifier:“cell”,for:indexath)
cell.contentView.backgroundColor=.blue
返回单元
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize{
返回CGSize(宽:100,高:100)
}
func scrollView将开始加速(scrollView:UIScrollView){
打印(“泛型类:\(\函数)”)
}
func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){
打印(“泛型类:\(\函数)”)
}
}
//标记:-子类
类NewView:GenericView{
//叫
重写func scrollView将开始加速(scrollView:UIScrollView){
打印(“新类:\(\函数)”)
}
//叫
重写func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){
打印(“新类:\(\函数)”)
}
//叫
@objc func ScrollViewDiEndDecelling(uScrollView:UIScrollView){
打印(“新类:\(\函数)”)
}
//不打电话
func scrollView将开始刷新(scrollView:UIScrollView){
打印(“新类:\(\函数)”)
}
//不打电话
func collectionView(collectionView:UICollectionView,didHighlightItemAt indexPath:indexPath){
打印(“新类:\(\函数)”)
}
//不打电话
@objc func collectionView(collectionView:UICollectionView,willDisplay单元格:UICollectionViewCell,forItemAt indexPath:indexPath){
打印(“新类:\(\函数)”)
}
}
尽管
UICollectionViewDelegateFlowLayout
继承自
UIScrollViewDelegate
,但它们在上述代码中具有不同的行为

我发现这个:。该漏洞于2016年报告

这似乎不是一个
UICollectionView
问题,而是一个存在于Swift和Objective-C交互中的问题


我想知道这是一个将来会被解决的问题,还是一个特性,我应该阻止以这种方式编写代码。这真的让我困惑,浪费了很多时间。泛型类型是一个很好的想法,在编程中被广泛使用

问题是
UICollectionViewDelegateFlowLayout
delegated正在设置为
GenericView
的委托。但是
NewView
还有另一组方法未确认为
collectionView
UICollectionViewDelegateFlowLayout
。要使其正常工作,您需要在
NewView

中的
UICollectionViewDelegateFlowLayout
方法前面添加前缀
override
,问题是
UICollectionViewDelegateFlowLayout
委托被设置为
GenericView
的委托。但是
NewView
还有另一组方法未确认为
collectionView
UICollectionViewDelegateFlowLayout
。要使其工作,您需要在
NewView

中的
UICollectionViewDelegateFlowLayout
方法前面添加前缀
override
,如果我先在
GenericView
中实现
UICollectionViewDelegateFlowLayout
方法,然后在
NewView
。但是,我没有在
GenericView
中实现委托方法,而是尝试在
NewView
中直接实现委托方法。在这种情况下,当滚动视图委托方法具有
@objc
前缀时,就会调用它们。但是,无论集合视图委托方法是否具有
@objc
前缀,它们都不会被调用。由于
UICollectionViewDelegateFlowLayout
继承自
UIScrollViewDelegate
,我认为它们应该具有相同的行为