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

时间:2017-12-29 23:59:04

标签: ios swift uitableview uicollectionview uicollectionviewcell

问题

我正在尝试在视图控制器中使用卡片的集合视图。当用户点击卡片时,它会扩展。在任何时候,每张卡都有一张桌面视图,无论是否展开。我在表格视图中加载了数据,但只有当我点击一张卡片才能展开它时,或者我在屏幕上滚动收集视图卡片并返回屏幕时。什么是更简洁的工作流程,将tableviews放在每个集合视图单元格中?

这是我的主视图控制器:

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

这是我所关注的tableview单元格的子类。它只在滚动屏幕或点击集合视图单元格时显示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
    }

}

也许我根本不应该使用tableview?非常感谢所有帮助的人。我很容易接受批评,也喜欢从错误中学习(有点经常)。 :)

1 个答案:

答案 0 :(得分:0)

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

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

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

  • 从UICollectionViewCell派生 - MyCollectionViewCell - 此步骤是必需的。
  • 在MyCollectionViewCell的xib内部,插入一个启用了水平滚动的tableview。 (这是xib属性编辑器中的一个选项)
  • 如果需要自定义,也可以从UITableViewCell - MyTableViewCell派生。此步骤是可选的。
  • 在MyCollectionViewCell类文件中,具有表视图数据源和委托方法的实现。这样,每张卡都将其所有表格视图单元格子元素放在一个地方
  • 如果存在,请在MyTableViewCell类文件中定义自定义逻辑
  • 这里没有必要继承UITableView。需要定制的所有内容都可以使用子类化单元格完成。
相关问题