简体   繁体   English

集合视图单元格内的表视图?

[英]Table views inside collection view cells?

The Issue 问题

I am attempting to use a collection view in a view controller for cards. 我正在尝试在卡片的视图控制器中使用集合视图。 When a user taps on a card, it expands. 当用户点击卡片时,卡片会展开。 At all times, I have a tableview in each card, whether it is expanded or not. 无论何时,无论是否展开,我在每张卡中都具有一张表格视图。 I have the data loading in the table views, but only when I tap on a card to expand it or if I scroll collection view cards offscreen and back onscreen. 我在表格视图中加载了数据,但是只有当我点击某个卡片以将其展开时,或者当我在屏幕外和屏幕后滚动收集视图卡片时,才加载数据。 What is a cleaner workflow to doing this that puts tableviews in each collection view cell? 将表视图放在每个集合视图单元格中的清洁工作流程是什么?

This is in my main view controller: 这是在我的主视图控制器中:

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "productionBinderCell", for: indexPath) as? ProductionBinderCollectionViewCell

    let suck = cell?.detailsTableView.cellForRow(at: IndexPath(row: 0, section: 0)) as? DescriptionTableViewCell

    suck?.descriptionText.text = productions[indexPath.row].productionDescription

    cell?.detailsTableView.reloadData()

    cell?.layer.shouldRasterize = true
    cell?.layer.rasterizationScale = UIScreen.main.scale

    let title = productions[indexPath.row].productionTitle

    let luck = productions[indexPath.row].productionImage

    let zuck = UIImage(data:luck!)

    cell?.productionTitle.text = title

    cell?.productionFeaturedImage.image = zuck

    cell?.productionColorTint.tintColor = UIColor.blue

    let backbtn = cell?.viewWithTag(1)

    backbtn?.isHidden = true

    return cell!
}

This is in a subclass of the tableview: 这在tableview的子类中:

override func layoutSubviews() {
    super.layoutSubviews()
    self.dataSource = self
    self.delegate = self
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "productionDescription", for: indexPath) as? DescriptionTableViewCell
    return cell!
}

This is in a subclass of the tableview cell I am concerned with. 这是我关注的tableview单元的子类。 It only shows the uilabel text when scrolling offscreen or tapping on the collection view cell: 仅在屏幕外滚动或点击集合视图单元格时,才显示uilabel文本:

class DescriptionTableViewCell: UITableViewCell {

@IBOutlet var descriptionText: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    }

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

    // Configure the view for the selected state
    }

}

Maybe I shouldn't be using a tableview at all? 也许我根本不应该使用tableview? Thank you very much to all who help. 非常感谢所有帮助的人。 I am very open to criticism and love learning from my mistakes (a little too often). 我非常乐于接受批评,也喜欢从错误中学习(经常出错)。 :) :)

What you need, first of all, is clear definition of views and cells. 首先,您需要明确定义视图和单元格。

  • UICollectionview is a collection of UICollectionViewCell objects UICollectionview是UICollectionViewCell对象的集合
  • UITableView is a collection of UITableViewCell objects UITableView是UITableViewCell对象的集合

Do you need to embed entire table view within each card? 您是否需要在每张卡中嵌入整个表格视图? There you have your answer. 在那里,您有答案。 You have parent-child relationship between your UICollectionViewCell and UITableView derived classes: 您的UICollectionViewCell和UITableView派生类之间具有父子关系:

  • Derive from UICollectionViewCell - MyCollectionViewCell - this step is mandatory. 从UICollectionViewCell-MyCollectionViewCell派生-此步骤是必需的。
  • Inside xib for MyCollectionViewCell, insert a tableview, with horizontal scrolling enabled. 在xib for MyCollectionViewCell的内部,插入一个表视图,并启用水平滚动。 (this is an option inside xib attribute editor) (这是xib属性编辑器中的一个选项)
  • If needed to customize, also derive from UITableViewCell - MyTableViewCell. 如果需要自定义,还可以从UITableViewCell-MyTableViewCell派生。 This step is optional. 此步骤是可选的。
  • Inside MyCollectionViewCell class file, have implementation for table view datasource and delegate methods. 在MyCollectionViewCell类文件中,具有表视图数据源和委托方法的实现。 That way, each card will have all its table view cell children in one place 这样,每张卡都会将其所有表视图单元格子元素集中在一个位置
  • If present, define custom logic inside MyTableViewCell class file 如果存在,请在MyTableViewCell类文件中定义自定义逻辑
  • There is no need to subclass UITableView here. 此处无需子类化UITableView。 Everything that needs to be customized can be done using subclassing cells. 可以使用子类化单元完成需要自定义的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM