【问题标题】:How to display titles of cells from json file + server?如何显示来自json文件+服务器的单元格标题?
【发布时间】:2021-10-14 07:14:50
【问题描述】:

我需要在 tableView 中显示 12 个单元格。前 4 个单元格的标题取自 json 文件(它是旧代码)。例如:

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

应该从服务器显示其他 8 个单元格(它们的标题)。我成功获取了 mainCategories 数组中的数据(在打印中检查),但无法在 tableView 中显示它们。我得到致命错误:索引超出范围:文件 Swift/ContiguousArrayBuffer.swift,第 444 行您能说一下怎么回事吗?

enum WatchesCell: Int, CaseIterable {
    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
}

func tableView(_ tableView: UITableView,
               numberOfRowsInSection section: Int) -> Int {

    return WatchesCell.allCases.count
}

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
    case WatchesCell.TVShow.rawValue:

        let cell = tableView.dequeueReusableCell(withIdentifier: "PosterWithTitleTableViewCell",
                                                 for: indexPath) as! PosterWithTitleTableViewCell
        cell.setupSectionTitle(section.localized.uppercased(), viewModel.tvShow.count == 0)
        //print("Section 6: \(section)")
        return cell

    case WatchesCell.Educational.rawValue:

        let cell = tableView.dequeueReusableCell(withIdentifier: "PosterWithTitleTableViewCell",
                                                 for: indexPath) as! PosterWithTitleTableViewCell
        cell.setupSectionTitle(section.localized.uppercased(), viewModel.educational.count == 0)
        //print("Section 7: \(section)")
        return cell

    case WatchesCell.InfoAnalitycal.rawValue:

        let cell = tableView.dequeueReusableCell(withIdentifier: "PosterWithTitleTableViewCell",
                                                 for: indexPath) as! PosterWithTitleTableViewCell
        cell.setupSectionTitle(section.localized.uppercased(), viewModel.infoAnalytical.count == 0)
        //print("Section 8: \(section)")
        return cell

    case WatchesCell.InfoEntertainment.rawValue:

        let cell = tableView.dequeueReusableCell(withIdentifier: "PosterWithTitleTableViewCell",
                                                 for: indexPath) as! PosterWithTitleTableViewCell
        cell.setupSectionTitle(section.localized.uppercased(), viewModel.infoEntertainment.count == 0)
        //print("Section 9: \(section)")
        return cell

    case WatchesCell.Journalism.rawValue:

        let cell = tableView.dequeueReusableCell(withIdentifier: "PosterWithTitleTableViewCell",
                                                 for: indexPath) as! PosterWithTitleTableViewCell
        cell.setupSectionTitle(section.localized.uppercased(), viewModel.journalism.count == 0)
        //print("Section 10: \(section)")
        return cell

    case WatchesCell.Cultural.rawValue:

        let cell = tableView.dequeueReusableCell(withIdentifier: "PosterWithTitleTableViewCell",
                                                 for: indexPath) as! PosterWithTitleTableViewCell
        cell.setupSectionTitle(section.localized.uppercased(), viewModel.cultural.count == 0)
        //print("Section 11: \(section)")
        return cell

    case WatchesCell.Movies.rawValue:

        let cell = tableView.dequeueReusableCell(withIdentifier: "PosterWithTitleTableViewCell",
                                                 for: indexPath) as! PosterWithTitleTableViewCell
        cell.setupSectionTitle(section.localized.uppercased(), viewModel.movies.count == 0)
        //print("Section 12: \(section)")
        return cell

默认值: 返回 UITableViewCell() }

我还尝试从 json 文件中填充这 8 个标题,如前 4 个,并显示所有带有内容的单元格,但我需要从服务器动态显示标题。

【问题讨论】:

  • 您与switch indexPath.row 共享的上述代码看起来不完整。开关盒不完整。你需要处理所有的情况。请分享func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell的整个代码块以便更好地理解。
  • 添加 cellForRow 的完整代码
  • 您能告诉我们您的项目中哪个代码位于第 444 行,您遇到了这个问题吗?你能分享那个关节吗
  • 默认:返回 UITableView.automaticDimension }
  • 这个错误会不会和tableView.reloadData有关?

标签: arrays swift tableview


【解决方案1】:

在您的问题中,cellForRowAt 功能不完整。如果那是您当前的代码,那么您在项目中拥有什么。 fatal error: Index out of range 的问题是有道理的。 您的 switch case 不能处理所有索引路径方案。 你的cellForRowAt 应该是这样的:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let section = viewModel.mainCategories[indexPath.row].localized_name
    switch indexPath.row {
    case WatchesCell.NowWatching.rawValue:
        // Your Code
    case WatchesCell.Premiers.rawValue:
        // Your Code
    case WatchesCell.Recommendations.rawValue:
        // Your Code
    case WatchesCell.Popular.rawValue:
        // Your Code
    case default:
        // Handle all you code for 8 titles which should be displayed from server
        let cell = tableView.dequeueReusableCell(withIdentifier: "PosterWithTitleTableViewCell", for: indexPath) as! PosterWithTitleTableViewCell
        cell.setupSectionTitle(section.localized.uppercased(), viewModel.documentary.count == 0)
        print("Section: \(section)")
        return cell
    }
}

如果您的 indexPath.row 不是前 4 个索引,那么它将进入 default 开关盒。

【讨论】:

  • 嗨!看来你是对的。它仅在第一次加载数据时正确显示所有部分,然后添加 8 个黑色单元格。你能帮我如何处理所有 8 个标题吗?当我在您的代码之后添加第二个单元格时,它显示错误“定义与先前值冲突”
  • @Abrcd18 如果你能分享屏幕截图,那将是一个很大的帮助。
  • 修复错误,与加载数据有关
  • 听起来很棒??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
  • 2020-10-28
  • 2020-03-11
  • 2021-04-05
  • 1970-01-01
  • 2022-01-20
相关资源
最近更新 更多