【发布时间】:2018-09-21 13:06:35
【问题描述】:
我正在使用自定义类 CollectionViewConfigurator 以通用方式处理我的 CollectionViewCell 的配置。
它运行良好,这是示例类:
protocol ConfigurableCell {
static var reuseIdentifier: String { get }
associatedtype DataType
func configure(data: DataType)
}
extension ConfigurableCell {
static var reuseIdentifier: String { return String(describing: Self.self) }
}
protocol CellConfigurator {
static var reuseId: String { get }
func configure(cell: UIView)
var hash: Int { get }
}
class CollectionViewCellConfigurator<CellType: ConfigurableCell, DataType: Hashable>: CellConfigurator where CellType.DataType == DataType, CellType: UICollectionViewCell {
static var reuseId: String { return CellType.reuseIdentifier }
let item: DataType
init(item: DataType) {
self.item = item
}
func configure(cell: UIView) {
(cell as! CellType).configure(data: item)
}
var hash: Int {
return String(describing: CellType.self).hashValue ^ item.hashValue
}
}
extension Int: Diffable {
public var diffIdentifier: AnyHashable {
return self
}
}
注意:我受到了一篇非常好的文章的启发,该文章展示了 UITableView 的相同用法。我在我的UICollectionView 上尝试过,非常棒。
无论如何,我想处理这个UICollectionView中的拖放。
为此,如果我正确理解了委托方法,我在 UICollectionView 中的项目需要符合 NSItemProviderWriting 和 NSItemProviderReading 协议。
当我添加协议方法时,出现以下错误:
泛型类型不支持静态存储属性
然后我阅读了这个post 以了解错误并尝试绕过它。
但恐怕我正在深入研究该语言的一个非常复杂的领域。
有人可以解释我如何使用泛型来遵守这些协议吗?
【问题讨论】:
标签: ios swift generics drag-and-drop protocols