Ios 如何将现有集合视图添加到表视图而不丢失swift中的表视图单元格

Ios 如何将现有集合视图添加到表视图而不丢失swift中的表视图单元格,ios,swift,uicollectionview,tableview,uicollectionviewcell,Ios,Swift,Uicollectionview,Tableview,Uicollectionviewcell,我正在尝试将两个具有自己首选项的现有集合视图单元格添加到现有表视图的前两个单元格中,而不会丢失其他表视图单元格。现在,每个视图都可以手动正常工作。 集合V1: import UIKit final class CollectionViewCell: UICollectionViewCell { @IBOutlet weak var images: UIImageView! public func configureCollection(photo: UIImage) {

我正在尝试将两个具有自己首选项的现有集合视图单元格添加到现有表视图的前两个单元格中,而不会丢失其他表视图单元格。现在,每个视图都可以手动正常工作。

集合V1:

import UIKit

final class CollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var images: UIImageView!
    public func configureCollection(photo: UIImage) {
        images.image = photo
        images.layer.cornerRadius = 12
       
    }

}
第二辑:

import UIKit

final class SelectCollectionViewCell: UICollectionViewCell {
    
    @IBOutlet weak var selectionTextLabel: UILabel!
    public func confCollectionView(selectionText: String){
        selectionTextLabel.text = selectionText

    }
}
表视图

import UIKit

final class homePageTableView: UITableViewCell {
    @IBOutlet weak var productPhoto: UIImageView!
    @IBOutlet weak var productName: UILabel!
        public func configureHomeShop(photo: UIImage, item: String) {
        productName.text = item
        productPhoto.image = photo
    }
}
查看所有内容所在的控制器:

class homeView: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate{
    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var collectionViewOne: UICollectionView!
    @IBOutlet weak var collectionViewTwo: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionViewOne.register(CollectionViewCell.self, forCellWithReuseIdentifier: "itemsCollectionViewCell")
        collectionViewTwo.register(SelectCollectionViewCell.self, forCellWithReuseIdentifier: "SelectCollectionViewCell")
        collectionViewTwo.dataSource = self
        collectionViewTwo.delegate = self
        collectionViewOne.dataSource = self
        collectionViewOne.delegate = self
        tableView.dataSource = self
        tableView.delegate = self
    }
    // **Functions**

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return itemsArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "shopCell", for: indexPath) as? homePageTableView else {
            fatalError()
        }
        
        cell.configureHomeShop(photo: photoArrayTV.first!, item: itemsArray.first!)
        return cell
    }
    
    // MARK: - CollectionView

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView == self.collectionViewOne{
            return imagesArray.count
            
        }else{
            return selectionArray.count
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if collectionView == self.collectionViewOne{

            guard let cellOne = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCollectionViewCell", for: indexPath) as? CollectionViewCell else{
                fatalError()
            }
            cellOne.configureCollection(photo: imagesArray[imagesArray.count - indexPath.row - 1])
            return cellOne
            
        }else{

            guard let cellTwo = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCollectionViewCell", for: indexPath) as? SelectCollectionViewCell else{
                fatalError()
            }
            cellTwo.confCollectionView(selectionText: selectionArray.last!)
            return cellTwo
        }
        
    }
}

好的方法是,您必须在
tableviewsheader
中添加
collectionView

然后,每当您向上滚动您的
tableView
时,您的
collectionView
部分也将向上滚动

要在特定索引上加载传感器,请遵循以下步骤:

if(indexpath.row == 0){
//load first tableView cell with collection view 
}
else if (indexpath.row == 1){
//load second tableView cell with collection view
}
else{
//load table view cell here
}

看来这是个好主意。你能解释一下如何将集合视图添加到表格视图的标题中吗?你也可以在这个演示中将它们添加到单行单元格中,如下所示:看起来更容易将集合视图拖动到表格视图的顶部。它的工作是伟大的,但我不能拖动第二个集合视图的顶部。它在原型单元下移动。如何修复?请按照上面的链接修复集合视图的tableView索引0和1,以及tableView行的剩余索引。如果此答案对您有效,您可以将此答案标记为正确答案。我找不到如何设置单元格索引路径