получение индекса вне диапазона в numberOfRowsInSection - PullRequest
0 голосов
/ 28 января 2020

У меня есть странная cellModel в моем tableView, как это

   fileprivate enum CellModel {
    case search
    case categories
    case switcher
    case wishes
}

fileprivate enum HeaderModel {
    typealias CellType = CellModel
    case search
    case categories
    case switcher
    case wishes


    var cellModels: [MAWishesViewController.CellModel] {
        switch self {
        case .search: return [.search]
        case .categories: return [.categories]
        case .switcher: return [.switcher]
        case .wishes: return [.wishes]
        }
    }
}

private var wishes = [Wish]()
private var categories = [Category]()

private let models:[CellModel] = [.search, .categories, .switcher, .wishes]
private let HeaderModels: [HeaderModel] = [.search, .categories, .switcher, .wishes]

, и вот мой делегат и методы dataSource:) Я использую модель с enum, и я начал думать, что это плохая идея!

    func numberOfSections(in tableView: UITableView) -> Int {
    return models.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  let header = HeaderModels[section]
    switch header {
    case .categories:
        return 1
    case .search:
        return 1
    case .switcher:
        return 1
    case .wishes:
        return self.wishes.count
    }

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let model = HeaderModels[indexPath.section].cellModels[indexPath.row]


    switch model {
    case .search:
        if let cell = tableView.dequeueReusableCell(withIdentifier: SearchTableViewCell.name, for: indexPath) as? SearchTableViewCell {

            return cell
        }
    case .categories:
        if let cell = tableView.dequeueReusableCell(withIdentifier: CategoriesTableViewCell.name, for: indexPath) as? CategoriesTableViewCell {
            cell.collectionView.delegate = self
            cell.collectionView.dataSource = self
            return cell
        }
    case .switcher:
        if let cell = tableView.dequeueReusableCell(withIdentifier: SwitcherTableViewCell.name, for: indexPath) as? SwitcherTableViewCell {

            return cell
        }
    case .wishes:
        if let cell = tableView.dequeueReusableCell(withIdentifier: WishTableViewCell.name, for: indexPath) as? WishTableViewCell {

            let wish = self.wishes[indexPath.row]
            cell.pictureImageView.image = UIImage(named: wish.image!)
            cell.nameLabel.text = wish.name
            cell.descriptionLabel.text = wish.description
            return cell
        }
    }

    return UITableViewCell.init()
}

И я получил "INDEX OUT OF RANGE. Все, что я хочу, это вернуть wishes.count в секцию, называемую .wishes

Также я выхожу из индекса в методе cellForRowAt. Так что проблема может быть?

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