How to fix Index out of range error in this task?

Hello. Could you please help me how to correctly display data in tableView?

I need to display 12 cells in tableView. Titles of the first 4 cells are taken from json file (it's legacy code). For example:

     case WatchesCell.NowWatching.rawValue:
        let cell = tableView.dequeueReusableCell(withIdentifier: "PosterWithRatingWideTableViewCell",
                                                 for: indexPath) as! PosterWithRatingWideTableViewCell
        
        cell.setupSectionTitle("now_watching".localized.uppercased(), viewModel.nowWatchingList.count == 0)
        return cell

Other 8 cells (their titles) should be displayed from server. I successfully fetched data in mainCategories array (checked in print), but can't display them in tableView. I got Fatal error: Index out of range: file Swift/ContiguousArrayBuffer.swift, line 444 Could you please say what's wrong?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let section = viewModel.mainCategories[indexPath.row].localized_name
    
    switch indexPath.row {
    
    case WatchesCell.NowWatching.rawValue:
        let cell = tableView.dequeueReusableCell(withIdentifier: "PosterWithRatingWideTableViewCell",
                                                 for: indexPath) as! PosterWithRatingWideTableViewCell
        
        cell.setupSectionTitle("now_watching".localized.uppercased(), viewModel.nowWatchingList.count == 0)
        return cell
        
    case WatchesCell.Premiers.rawValue:
        let cell = tableView.dequeueReusableCell(withIdentifier: "PosterWithTitleTableViewCell",
                                                 for: indexPath) as! PosterWithTitleTableViewCell
        cell.setupSectionTitle("premiers".localized.uppercased(), viewModel.premieres.count == 0)
        return cell
        
    case WatchesCell.Recommendations.rawValue:
        let cell = tableView.dequeueReusableCell(withIdentifier: "PosterWithRatingWideTableViewCell",
                                                 for: indexPath) as! PosterWithRatingWideTableViewCell
        
        cell.setupSectionTitle("recommendations".localized.uppercased(), viewModel.recommendations.count == 0)
        return cell
        
    case WatchesCell.Popular.rawValue:
        let cell = tableView.dequeueReusableCell(withIdentifier: "PosterWithRatingWideTableViewCell",
                                                 for: indexPath) as! PosterWithRatingWideTableViewCell
        cell.setupSectionTitle("popular".localized.uppercased(), viewModel.populars.count == 0)
        return cell
        
   //Here's start cells, which should be displayed from server:
    case WatchesCell.Documentary.rawValue:
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "PosterWithTitleTableViewCell",
                                                 for: indexPath) as! PosterWithTitleTableViewCell
        cell.setupSectionTitle(section.localized.uppercased(), viewModel.documentary.count == 0)
        //print("Section 5: \(section)")
        return cell

I also tried to fill these 8 titles like the first 4, from json file, and all cells with content are displayed but I need to show titles dynamically, from server.

You haven't implemented…

  public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
  {
    return WatchesCell.allCases.count
  }

Assuming the WatchesCell enum implements CaseIterable

Oh thank you. In numberOfRows I did:

return (4 + viewModel.mainCategories.count)

This's WatchCell enum:

enum WatchesCell: Int {
case NowWatching = 0
case Premiers
case Recommendations
case Popular
case Documentary
case TVShow
case Educational
case InfoAnalitycal
case InfoEntertainment
case Journalism
case Cultural
case Movies
}

Added CaseIterable to enum and changed numberOfRowsInSection like you suggested but still got this error. :(

Terms of Service

Privacy Policy

Cookie Policy