Как написать определение класса для этого в Swift? - PullRequest
1 голос
/ 27 января 2020

Я пытаюсь реализовать обобщенный 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
    }
}

1 Ответ

1 голос
/ 27 января 2020

Вы можете сделать что-то вроде этого

import UIKit

class MyViewModel {

}

class MyCell: UITableViewCell, TableCellDelegate {
    static var cellIdentifier: String = "id"

    func configureCell(withViewModel viewModel: MyViewModel) {

    }

    typealias ViewModel = MyViewModel


}

protocol TableCellDelegate: UITableViewCell {
    associatedtype ViewModel
    func configureCell(withViewModel viewModel:ViewModel)
    static var cellIdentifier: String {get}
}


class TableViewDataSource<CellType>:NSObject, UITableViewDataSource, UITableViewDelegate where CellType: TableCellDelegate {

    var items:[CellType.ViewModel]

    init(viewModelItems: [CellType.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.cellIdentifier, for: indexPath) as? CellType else {
            fatalError("Could not initialize cell for identifier \(CellType.cellIdentifier)")
        }
        cell.configureCell(withViewModel:items[indexPath.row])
        return cell
    }
}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...