【发布时间】: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有关?