【发布时间】:2015-07-03 14:32:37
【问题描述】:
当我尝试创建一个在 swift 中实现 UICollectionViewDataSource 的通用类时,它说我的类不符合协议(有时 Xcode 崩溃)。
这是否意味着我们不能为 UICollectionView 创建通用数据提供者并且我们必须复制代码?
这是通用代码:
// Enum protocol
protocol OptionsEnumProtocol
{
typealias T
static var allValues:[T] {get set}
var description: String {get}
func iconName() -> String
}
// enum : list of first available options
enum Options: String, OptionsEnumProtocol
{
typealias T = Options
case Color = "Color"
case Image = "Image"
case Shadow = "Shadow"
static var allValues:[Options] = [Color, Image, Shadow]
var description: String {
return self.rawValue
}
func iconName() -> String
{
var returnValue = ""
switch(self)
{
case .Color: returnValue = "color_icon"
case .Image: returnValue = "image_icon"
case .Shadow: returnValue = "shadow_icon"
}
return returnValue
}
}
// class to use as the uicollectionview datasource and delegate
class OptionsDataProvider<T>: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
private let items = T.allValues
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return items.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(OptionsCellReuseIdentifier, forIndexPath: indexPath) as! GenericIconLabelCell
let item = self.items[indexPath.row]
// Configure the cell
cell.iconFileName = item.iconName()
cell.labelView.text = item.description
return cell
}
}
但因为失败了,我不得不改用这种非通用形式:
enum Options: String
{
case Color = "Color"
case Image = "Image"
case Shadow = "Shadow"
static var allValues:[Options] = [Color, Image, Shadow]
var description: String {
return self.rawValue
}
func iconName() -> String
{
var returnValue = ""
switch(self)
{
case .Color: returnValue = "color_icon"
case .Image: returnValue = "image_icon"
case .Shadow: returnValue = "shadow_icon"
}
return returnValue
}
}
class OptionsDataProvider: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
private let items = Options.allValues
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return items.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(OptionsCellReuseIdentifier, forIndexPath: indexPath) as! GenericIconLabelCell
let item = self.items[indexPath.row]
// Configure the cell
cell.iconFileName = item.iconName()
cell.labelView.text = item.description
return cell
}
}
这使我有义务为我拥有的每种枚举类型复制该类。
确切的错误:
【问题讨论】:
-
为什么要投反对票?我可以有理由吗?
-
您能否说明您想要实现的目标?我问是因为如果您创建一个采用 UICollectionViewDelegate 和 UICollectionViewDataSource 的所有方法的 NSObject 子类,您将能够将其用作集合视图的委托和数据源。所以,不清楚你想要实现什么
-
是的,我已经将它用作我的 uicollectionview 的委托和数据源。为此,它有效。问题是我将这个“委托/数据源”类复制到我的每个数据类型。我的意思是,我使用枚举作为数据,我必须为每个枚举创建一个类。但我只想创建一个“委托/数据源”并将枚举类型指定为 T 参数。我会更新我的问题让你理解。
-
我更新了我的问题,让您更好地了解我想要实现的目标。
标签: ios xcode swift generics uicollectionview