gpt4 book ai didi

swift - 如何以编程方式将 UICollectionView 插入 UITableViewCell ? [ swift ]

转载 作者:行者123 更新时间:2023-11-30 10:47:30 25 4
gpt4 key购买 nike

有谁知道如何将collectionView集成到tableView的顶部部分?我试图获取具有水平滚动图像的顶部部分/行,而其下方的其他部分/行只是普通的表格 View 行。就像您在 Facebook Messenger 或 Tinder/bumble 的比赛/对话页面等应用程序中可能看到的内容一样。

我了解如何分别生成 UICollectionView 和 UITableView,但在多个函数/委托(delegate)/数据源之间,我不知道如何正确分层代码。

我正在以编程方式工作,而不是在 Storyboard上工作。我知道还有其他关于在 UITableViewCell 中插入 UICollectionView 的帖子,但它们要么是 Storyboard实现,要么是过时的 Swift 版本 - 如果有人可以进行一次运行,以便将来的任何人也可以理解逻辑,那就太棒了即使某些代码已经过时。

最佳答案

  1. 在 TableViewCell swift 文件中声明 collectionView。

    var viewPhotos: UICollectionView!

    func reset(with cellData: CellData) {
    self.data = cellData
    viewPhotos.dataSource = self
    viewPhotos.delegate = self
    viewPhotos.reloadData()
    }
  2. init 函数或 awakeFromNib 中检查 photoCollectionView 是否已初始化并将其添加到单元格中。

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
viewPhotos = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
viewPhotos.collectionViewLayout = layout
viewPhotos.showsHorizontalScrollIndicator = false
viewPhotos.isPagingEnabled = true
viewPhotos.register(PhotoCollectionViewCell.self, forCellWithReuseIdentifier: PhotoCollectionViewCell.cellIdentifier)
  • 将 UICollectionView 数据源和委托(delegate)添加到 UITableViewCell
  • extension TableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {  

    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection > section: Int) -> Int {
    return photoUrls?.count ?? 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell: PhotoCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: PhotoCollectionViewCell.cellIdentifier, for: indexPath) as! PhotoCollectionViewCell

    if let value = photoUrls {
    cell.reset(with url: value[indexPath.row]);
    }
    return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: kPhotoWidth, height: kPhotoHeight)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

    return UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)

    }

    func collectionView(_ collectionView: UICollectionView, layout > collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0.0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0.0
    }

    }
  • PhotoCollectionViewCell
  • create a PhotoCollectionViewCell derived from UICollectionViewCelland add a UIImageView, and make all edge constraints to 0 to thesuperview. load UIImageView based on photoUrl.

    关于swift - 如何以编程方式将 UICollectionView 插入 UITableViewCell ? [ swift ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55462176/

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