gpt4 book ai didi

ios - UITableView 中嵌入的 UICollectionView 中的 UICollectionViewCell 未按预期显示

转载 作者:行者123 更新时间:2023-11-30 11:30:24 25 4
gpt4 key购买 nike

我在 UICollectionViewController 的 Collection View 中显示自定义 UICollectionViewCell 时遇到问题。

潜在的重要因素:

  • 我以编程方式创建布局,而不是使用 Storyboard
  • Collection View 作为 UITableViewCell 的 subview 嵌入到我的主 UIViewController 的 UITableView 中。
  • 我正在使用水平方向的 UICollectionViewFlowLayout
  • 单元格填满其父单元格的整个宽度和高度。

我在测试中注意到的事情:

  • 在应用的当前状态下,仅针对应显示的第一个单元格调用 cellForItemAt indexPath 一次。
  • 点击第一个单元格后,它就会消失。我还没有找到让它重新出现的方法。
  • 我可以滑动到其他单元格应该所在的位置,并且那里有正确数量的“空槽”,但单元格本身没有出现。

我试图只包含我认为必要的代码,以减少这篇文章的大小,但如果有什么我没有考虑到,请告诉我。感谢您的任何帮助或建议。此外,我希望将来在此类情况下使用 Xcode 进行调试时能得到一些指导。有谁知道有任何指南或书籍提供了解决此类问题的技巧吗?我对 gdb/Visual Studio 的调试工具有一些经验,但是 Xcode 和框架在调试时对我来说有点困难。再次感谢。

表格 View 的定义和约束:

...

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")
}
}

最佳答案

需要确保 Collection View Controller 在范围内。它的范围最初仅限于定义 Collection View 的代码块,因此 Collection View 不再具有数据源或委托(delegate)。

关于ios - UITableView 中嵌入的 UICollectionView 中的 UICollectionViewCell 未按预期显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50321783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com