ios - UITableViewCell 内部的 UICollectionView

标签 ios swift uitableview uicollectionview

我正在尝试在 TableViewCell 中设置 CollectionView。我已经通读了一大堆问题、tuts 和视频,到目前为止,我的方法似乎是正确的,但我的 Collection View 仍然没有加载到我的表格 View 单元格中。

代码:

import UIKit

class TheaterCell: UITableViewCell {

@IBOutlet var theaterName: UILabel!

@IBOutlet var theaterLocation: UILabel!

@IBOutlet var showtimesCollection: UICollectionView!

override func awakeFromNib() {
    super.awakeFromNib()

    showtimesCollection.delegate = self
    showtimesCollection.dataSource = self
    showtimesCollection.reloadData()

}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

extension TheaterCell: UICollectionViewDataSource, UICollectionViewDelegate {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return 10

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = showtimesCollection.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! TimeCell

    cell.time.text = "1:00"

    return cell

}

}

Tableview 从 ViewController 加载并显示其单元格和元素,但 Collection View 未加载到单元格中。

最佳答案

这对我有用,我认为问题出在您注册手机的方式上

class YourCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var collectionView: UICollectionView!

override func awakeFromNib() {
    super.awakeFromNib()
    registerCell()
    self.collectionView.delegate = self
    self.collectionView.dataSource = self
}
func registerCell() {
    collectionView.register(TimeCell.self, forCellWithReuseIdentifier: "cell")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TimeCell
    cell.time.text = "1:00"
    return cell
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

关于ios - UITableViewCell 内部的 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53698093/

相关文章:

ios - 具有自定义配置的 UICollectionView 列表 - 如何将单元格中的更改传递给 View Controller ?

ios - 从 IOS 上传图像文件到 Web 服务器

jquery - 自定义单选按钮 - 在 iPad 上的 Safari 中显示隐藏的默认按钮

ios - 按下以编程方式创建的新按钮时如何继续?

ios - Swift - JSON 数据不会显示在 UITableview 中

ios - 拉取刷新插件: PullToBounce Wrapper UIScrollView

ios - 如何使表格 View 对齐到单元格中心? (附gif动画图)

ios - 核心数据获取上的 EXC_BAD_ACCESS(code=1, address=0x10)

ios - 从另一种方法 iOS 更新 UITableViewCell 中的值

ios - 在 UITableView 中滚动后 UIButton 颜色发生变化