【问题标题】:UICollectionView inside of UITableViewCellUITableViewCell 内部的 UICollectionView
【发布时间】:2018-12-10 00:30:34
【问题描述】:

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

代码:

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 加载并显示其单元格和元素,但集合视图未在单元格中加载。

【问题讨论】:

  • 您的表格视图单元格出现了吗?
  • 是的,如问题中所述,表格视图加载并显示单元格。集合视图不会在单元格中加载。
  • 显示表格视图的数据源方法

标签: ios swift uitableview uicollectionview


【解决方案1】:

这对我有用,我认为问题来自您注册手机的方式

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
}

【讨论】:

  • 我收到一个控制台错误:无法将“UICollectionViewCell”(0x10e36bea0)类型的值转换为“Film_Bee.TimeCell”(0x10533f688)。
  • 我刚刚再次检查了我的表格视图,但我没有以编程方式设置行的大小。在没有 registerCell 函数的情况下与您的代码一起执行此操作。
【解决方案2】:

我的工作是使用故事板并通过拖入类来设置故事板中的代表和数据源。

a) 将 TableView 的委托和数据源设置为 ViewController

b) 将 CollectionView 的委托和数据源设置为 TableViewCell(TheaterCell)

ViewController 代码:

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}
}

extension ViewController:UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return  1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let theaterCell:TheaterCell = tableView.dequeueReusableCell(withIdentifier: "TheaterCell", for: indexPath) as! TheaterCell
    theaterCell.reloadCollectionView()
    return theaterCell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 200
}
}

剧院单元代码:

class TheaterCell: UITableViewCell {
@IBOutlet var showtimesCollection: UICollectionView!
override func awakeFromNib() {
    super.awakeFromNib()
}

func reloadCollectionView() -> Void {
    self.showtimesCollection.reloadData()
}
}

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

时间单元代码:

class TimeCell: UICollectionViewCell {
    @IBOutlet var time: UILabel!
}

这是输出:

注意:如果您不使用故事板并且仅从代码制作收藏视图或表格,那么您已将您的手机注册为: A) TheaterCell 必须注册到 ViewController 类 B) TimeCell 必须注册到 TheaterCell 类中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多