gpt4 book ai didi

ios - swift 从 UICollectionViewDataSource 继承时的通用类

转载 作者:搜寻专家 更新时间:2023-10-30 21:55:25 24 4
gpt4 key购买 nike

当我尝试创建一个在 swift 中实现 UICollectionViewDataSource 的通用类时,它说我的类不符合协议(protocol)(有时 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
}
}

这使我有义务为我拥有的每个枚举类型复制类。

确切错误:

enter image description here

最佳答案

你是对的,不可能写一个泛型类。但是,我找到了解决方法。它不使用枚举,因此您可能不会觉得它很有用。然而,它实现了你想要的——你得到了一个 Collection View 数据源,它可以与提供必要数据的不同类一起使用。这是代码:

protocol OptionsProviderProtocol
{
func allValues() -> [OptionsItem]
}

class OptionsItem:NSObject {
let itemDescription:String
let iconName:String

init(iconName:String,description:String) {
self.itemDescription = description
self.iconName = iconName
}
}

// class stores first available options
class Options: NSObject, OptionsProviderProtocol
{

let color = OptionsItem(iconName: "color_icon", description: "Color")
let image = OptionsItem(iconName: "image_icon", description: "Image")
let shadow = OptionsItem(iconName: "shadow_icon", description: "Shadow")

func allValues() -> [OptionsItem] {
return [color, image, shadow]
}
}

// class to use as the uicollectionview datasource and delegate
class OptionsDataProvider: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
private var items:[OptionsItem] = []

convenience init(optionsProvider:OptionsProviderProtocol) {
self.items = optionsProvider.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
}
}

如果您有任何问题,请告诉我。

关于ios - swift 从 UICollectionViewDataSource 继承时的通用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31209235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com