gpt4 book ai didi

ios - Collection View 单元格内的 TableView ?

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

问题

我正在尝试在卡片 View Controller 中使用 Collection View 。当用户点击卡片时,它就会展开。在任何时候,我在每张卡中都有一个表格 View ,无论它是否展开。我在表格 View 中加载了数据,但仅当我点击卡片将其展开或将 Collection View 卡片滚动到屏幕外并返回屏幕时才加载。将表格 View 放入每个 Collection 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!
}

这是 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!
}

这是我关心的表格 View 单元的子类。它仅在滚动到屏幕外或点击 Collection View 单元格时显示 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
}

}

也许我根本不应该使用表格 View ?非常感谢所有提供帮助的人。我非常愿意接受批评,并且喜欢从错误中学习(有点太频繁了)。 :)

最佳答案

首先,您需要的是 View 和单元格的明确定义。

  • UICollectionview 是 UICollectionViewCell 对象的集合
  • UITableView 是 UITableViewCell 对象的集合

您需要在每张卡片中嵌入整个表格 View 吗?这就是你的答案。您的 UICollectionViewCell 和 UITableView 派生类之间具有父子关系:

  • 从 UICollectionViewCell - MyCollectionViewCell 派生 - 此步骤是强制性的。
  • 在 MyCollectionViewCell 的 xib 内,插入一个启用水平滚动的表格 View 。 (这是 xib 属性编辑器中的一个选项)
  • 如果需要自定义,也可以从 UITableViewCell - MyTableViewCell 派生。此步骤是可选的。
  • 在 MyCollectionViewCell 类文件中,具有 TableView 数据源和委托(delegate)方法的实现。这样,每张卡片都会将其所有 TableView 单元格子项放在一个位置
  • 如果存在,请在 MyTableViewCell 类文件中定义自定义逻辑
  • 这里不需要子类化 UITableView。所有需要定制的事情都可以使用子类化单元来完成。

关于ios - Collection View 单元格内的 TableView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48029677/

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