Я пытаюсь реализовать обобщенный c TableViewDataSource
, который можно использовать, предоставив массив ViewModels, и каждая ячейка должна иметь метод configureCell
для заполнения ячейки из предоставленного ViewModel
.
Я думаю, мне нужно добавить что-то еще к этой строке кода
class TableViewDataSource<CellType,ViewModel>:NSObject, UITableViewDataSource where CellType: UITableViewCell
, чтобы получить доступ
cell.configureCell(withViewModel:items[indexPath.row])
Это весь класс для справки:
protocol TableCellDelegate {
associatedtype ViewModel
func configureCell(withViewModel viewModel:ViewModel)
}
class TableViewDataSource<CellType,ViewModel>:NSObject, UITableViewDataSource where CellType: UITableViewCell {
var items:[ViewModel]
init(viewModelItems: [ViewModel]) {
self.items = viewModelItems
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: CellType.identifier, for: indexPath) as? CellType else {
fatalError("Could not initialize cell for identifier \(CellType.identifier)")
}
cell.configureCell(withViewModel:items[indexPath.row])
return cell
}
}