ios - 嵌入在 UITableView 中的 UICollectionView 中的 UICollectionViewCells 未按预期显示
问题描述
我在 UICollectionViewController 的集合视图中显示自定义 UICollectionViewCells 时遇到问题。
潜在的重要因素:
- 我正在以编程方式创建布局,而不是使用情节提要
- 集合视图作为 UITableViewCell 的子视图嵌入到我的主 UIViewController 中的 UITableView 中。
- 我正在使用水平方向的 UICollectionViewFlowLayout
- 一个单元格填满其父单元的整个宽度和高度。
我在测试中注意到的事情:
- 在应用程序的当前状态下,cellForItemAt indexPath 仅对应显示的第一个单元格调用一次。
- 当第一个单元格被点击时,它会消失。我还没有找到让它重新出现的方法。
- 我可以滑动到其他单元格应该在的位置,并且那里有正确数量的“空槽”,但单元格本身不会出现。
我试图只包含我认为必要的代码来减少这篇文章的大小,但是如果有什么我没有考虑到,请告诉我。感谢您提供任何帮助或建议。此外,我希望在将来在这种情况下使用 xcode 进行调试时得到一些指导。有谁知道任何指南或书籍可以提供寻找此类问题的提示?我对 gdb/visual studio 的调试工具有一些经验,但是在调试方面,Xcode 和框架对我来说有点困难。再次感谢。
表视图的定义和约束:
...
let trainingTableViewController = TrainingTableViewController()
lazy var trainingTableView: UITableView = {
let tableView = UITableView()
tableView.dataSource = trainingTableViewController
tableView.delegate = trainingTableViewController
tableView.register(TrainingLogoCell.self, forCellReuseIdentifier: "trainingLogoID")
tableView.register(TrainingProgressCell.self, forCellReuseIdentifier: "trainingProgressID")
tableView.register(TrainingProgressPageControlCell.self, forCellReuseIdentifier: "trainingProgressPageControlID")
tableView.register(TrainingWeekCell.self, forCellReuseIdentifier: "trainingWeekID")
tableView.separatorStyle = .none
tableView.backgroundColor = .clear
tableView.translatesAutoresizingMaskIntoConstraints = false
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
...
view.addSubview(trainingTableView)
trainingTableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true // TODO: see about anchoring to bottom of navigation
trainingTableView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 25).isActive = true
trainingTableView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -25).isActive = true
trainingTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
具有 CollectionView 单元格的 TableView 类
class TrainingTableViewController: NSObject, UITableViewDelegate, UITableViewDataSource {
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "trainingLogoID", for: indexPath)
return cell
} else if indexPath.section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "trainingProgressID", for: indexPath)
return cell
}
else if indexPath.section == 2 {
let cell = tableView.dequeueReusableCell(withIdentifier: "trainingProgressPageControlID", for: indexPath)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "trainingWeekID", for: indexPath)
return cell
}
}
...
class TrainingProgressCell: UITableViewCell {
let trainingProgressCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
let collectionViewController = TrainingProgressCollectionViewController(collectionViewLayout: layout)
collectionViewController.collectionView!.isPagingEnabled = true
collectionViewController.collectionView!.backgroundColor = .clear
collectionViewController.collectionView!.translatesAutoresizingMaskIntoConstraints = false
return collectionViewController.collectionView!
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.heightAnchor.constraint(equalToConstant: 250).isActive = true
backgroundColor = UIColor.init(rgb: 0x333333)
layer.cornerRadius = 10
selectionStyle = UITableViewCellSelectionStyle.none
contentView.addSubview(trainingProgressCollectionView)
trainingProgressCollectionView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
trainingProgressCollectionView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
trainingProgressCollectionView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
trainingProgressCollectionView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
...
我的 UICollectionView 类
import UIKit
private let reuseIdentifier = "trainingProgressCollectionViewID"
class TrainingProgressCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.register(TrainingProgressCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print(String(indexPath.row))
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! TrainingProgressCollectionViewCell
cell.backgroundColor = .red
cell.dayLabel.text = "Day " + String(indexPath.row)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
}
}
class TrainingProgressCollectionViewCell: UICollectionViewCell {
var constraintsSetupDone = false
let dayLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = .white
label.font = UIFont(name: "Montserrat-ExtraBold", size: 18)
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(dayLabel)
dayLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
dayLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
解决方案
需要确保集合视图控制器在范围内。它的范围最初仅限于定义集合视图的代码块,因此,集合视图不再具有数据源或委托。
推荐阅读
- r - 如何制作嵌套循环以从列表中提取我需要的元素
- python - 在调用方法期间接收 DeferredAttribute 而不是值
- c++ - 允许继承类使用相同的功能
- javascript - 对比 chart.js 数据标签颜色与背景
- discord - 如何限制机器人可以发送给 1 个用户的消息
- c++ - opengl中的多个聚光灯不起作用
- font-awesome - 为什么字体在代码笔中很棒,但在我的本地文档中却不行?
- android - 如何修复 Android Studio 中的错误?
- amazon-web-services - 无法在 Linux 机器上解压缩 .zip 文件
- python - Django 查询在 shell 中工作,但 jinja 无法在模板中识别