【问题标题】:How can I programmatically insert UICollectionView into UITableViewCell ? [Swift]如何以编程方式将 UICollectionView 插入 UITableViewCell ? [迅速]
【发布时间】:2019-08-23 00:51:45
【问题描述】:

有谁知道如何将 collectionView 集成到 tableView 的顶部?我正在尝试获取具有水平滚动图像的顶部部分/行,而其下方的其他部分/行只是普通的 tableview 行。就像您在 facebook messenger 或 tinder/bumble 的比赛/对话页面等应用中看到的一样。

我了解如何分别生成 UICollectionView 和 UITableView,但是在多个函数/委托/数据源之间,我无法弄清楚如何正确分层代码。

我正在以编程方式而不是在情节提要上工作。我知道还有其他关于在 UITableViewCell 中插入 UICollectionView 的帖子,但它们要么是故事板实现,要么是在过时的 Swift 版本上——如果有人可以做一个遍历,这样任何人在未来也可以理解逻辑,那就太棒了即使某些代码已经过时。

【问题讨论】:

    标签: swift xcode uitableview uicollectionview


    【解决方案1】:
    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)
    
    1. 添加 UICollectionView 数据源并委托给 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
    }
    
    }
    
    1. PhotoCollectionViewCell

    创建一个从 UICollectionViewCell 派生的 PhotoCollectionViewCell 并添加一个 UIImageView,并将所有边缘约束设置为 0 超级视图。根据 photoUrl 加载 UIImageView。

    【讨论】:

    • collectionview 委托和数据源在哪里?
    • @ErkamKUCET,您可以在 awakeFromNib 方法中设置委托和数据源,也可以像我在步骤 1 中所做的那样定义 reset 方法。
    • 好的,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-01-22
    • 2022-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2021-05-02
    • 2017-08-22
    • 1970-01-01
    相关资源
    最近更新 更多