Попробуйте, Кредиты Шаблоны проектирования связанных типов Swift
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
}