我在UICollectionViewController的集合视图中显示自定义UICollectionViewCells时遇到问题。
潜在的重要因素:
我在测试中注意到的事情:
我试图只包含我认为必要的代码来减少这篇文章的大小,但如果有任何我没有考虑的事情,请告诉我。感谢您的任何帮助或建议。另外,我希望将来在这种情况下使用xcode进行调试时需要一些指导。有没有人知道任何指南或书籍提供了解决这样的问题的技巧?我对gdb / visual studio的调试工具有一些经验,但是在调试时,Xcode和框架对我来说有点难度。再次感谢。
表格视图的定义和约束:
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")
}
}
...
带有CollectionView单元格的TableView类
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")
}
}
我的UICollectionView类
{{1}}
答案 0 :(得分:0)
需要确保集合视图控制器在范围内。其范围最初仅限于定义集合视图的代码块,因此,集合视图不再具有数据源或委托。