swift - 如何创建通用的 UITableviewCell,它可以通过委托(delegate)或其他方式将通用数据类型传递给主 UIViewController?

标签 swift iphone uitableview generics delegates

我正在尝试创建一个通用的 UITableViewcell,它可以在创建单元格时将通用类型推断为具体类型,如下所示:

    protocol ConfigureCell {
        associatedtype DataType
        func configure(data:DataType)
    }

            class AbstractCell<T:Codable>: UITableViewCell , ConfigureCell  {

            func configure(data: T) {

            }

            var delegate : AbstractCellDelegate!
        }


class UserCell:AbstractCell<UserDetail> {

    override func configure(data: UserDetail) {

    }
}

    protocol AbstractCellDelegate {
     associatedtype DataType
    func cellBtnClicked(model:DataType,index:Int,cell:AbstractCell<DataType>)
    func cellBtnClicked(model:DataType,index:Int,cell:AbstractCell<DataType>,action:String)
    func cellBtnClicked(model:DataType?,index:Int,cell:AbstractCell<DataType>,action:String)
}

enter image description here

enter image description here

如果需要,我还有一些委托(delegate)可用于将相关数据传递给主 UIViewController。如果单元格不是通用的,则委托(delegate)工作正常,但在创建通用单元格后我无法使用委托(delegate)方法并出现以下错误。这可以通过类型删除来解决,但是因为我不太了解它们,所以这太过分了。有没有一种方法可以创建一个通用单元格,并且还有一种方法可以通过委托(delegate)或任何其他方式调用主 UIViewcontroller 类的方法。

最佳答案

试试这个,积分 Swift Associated Type Design Patterns

protocol ConfigureCell {
    associatedtype DataType
    func configure(data: DataType)
}

class AbstractCell<T: Codable>: UITableViewCell, ConfigureCell {
    func configure(data: T) {
    }

    var delegate: AbstractCellDelegate?
}

class UserCell: AbstractCell<UserDetail> {
    override func configure(data: UserDetail) {
    }
}

protocol AbstractCellDelegate {
    func cellBtnClicked(model: Codable, index: Int, cell: Any)
    func cellBtnClicked(model: Codable, index: Int, cell: Any, action: String)
    func cellBtnClicked(model: Codable?, index: Int, cell: Any, action: String)
}

extension AbstractCellDelegate {
    // dont need them
    func cellBtnClicked(model: Codable, index: Int, cell: Any) {}
    func cellBtnClicked(model: Codable, index: Int, cell: Any, action: String) {}
    func cellBtnClicked(model: Codable?, index: Int, cell: Any, action: String) {}
}

protocol CellDelegate: AbstractCellDelegate {
    associatedtype DataType: Codable
    func cellBtnClicked(model: DataType, index: Int, cell: AbstractCell<DataType>)
    func cellBtnClicked(model: DataType, index: Int, cell: AbstractCell<DataType>, action: String)
    func cellBtnClicked(model: DataType?, index: Int, cell: AbstractCell<DataType>, action: String)
}

struct UserDetail: Codable {
}

struct MyDataType: Codable {
}

class MyViewController: CellDelegate {
    func cellBtnClicked(model: MyDataType, index: Int, cell: AbstractCell<MyDataType>) {
    }

    func cellBtnClicked(model: MyDataType, index: Int, cell: AbstractCell<MyDataType>, action: String) {
    }

    func cellBtnClicked(model: MyDataType?, index: Int, cell: AbstractCell<MyDataType>, action: String) {
    }

    typealias DataType = MyDataType
}

编辑 你可以试试这个

protocol ConfigureCell {
    associatedtype DataType
    func configure(model: DataType)
}

protocol DelegateCell {
    associatedtype DataType
    func btnTap(model: DataType)
}

class UserCell: ConfigureCell, DelegateCell {
    typealias DataType = UserDetail

    var btnTapHandler: ((DataType) -> Void)?

    func btnTap(model: DataType) {
        btnTapHandler?(model)
    }

    var model: DataType?
    func configure(model: DataType) {
        self.model = model
    }

    @IBAction func tap() {
        if let model = model {
            btnTap(model: model)
        }
    }
}

struct UserDetail {
    var name = "Manas"
}

class TableViewController {

    var items = [UserDetail]()

    func cellForIndexPath(row: Int) {
        let cell = UserCell()
        cell.configure(model: items[row])
        cell.btnTapHandler = { data in
            print(data.name)
        }
    }

}

关于swift - 如何创建通用的 UITableviewCell,它可以通过委托(delegate)或其他方式将通用数据类型传递给主 UIViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55117711/

相关文章:

arrays - 将 Firestore 数据加载到 TableView Swift 4

swift - 快速剖析排列算法

swift - Heroku 上的 Vapor 3 : REPL?

swift - Xcode 7 - 启动屏幕本地化不起作用

iphone - 地址可达性 - 服务器和端口 - iOS 5

ios - 编程 .beginRefresh() 不刷新 UITableView 中的数据

ios - 发送到实例的 UITableViewCell copyWithZone 无法识别的选择器

ios - 如何通过 itunesconnect 从 TestFlight 安装应用程序作为 ios 中的内部测试器?

iphone - 实现 transient 属性

ios - UITableViewCell 中的阴影在向上滚动时从底部移动到顶部