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

标签 ios swift uicollectionview

我在 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/

相关文章:

swift - UICollectionView 单元格垂直排列而不是网格排列

ios - Mapkit 中未解析的标识符

ios - 首次加载时 SDWebImage 单元格 ImageView 大小错误

ios - 是否可以使用 sqlcipher 在 iOS 上使用全文搜索?

swift - 如何根据用于两个不同目的的 UITableViewController 中的不同条件设置 UITableViewAccessoryType?

ios - Swift 2,滚动后的 UiCollectionview - 更改的单元格数据

iphone - NSURL 连接在 3g 上失败 'network connection was lost'

ios - 在多个 UIImageView 上点击手势

ios - 如何使用 iOS Graph API 从 Facebook 相册中获取更大的图像?

ios - collectionview 在 tableview 单元格内重复