我正在尝试创建一个通用的 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)
}
如果需要,我还有一些委托(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/