【问题标题】:How to add existing collection views to table view without losing table view cells in swift如何在不丢失表格视图单元格的情况下将现有集合视图添加到表格视图
【发布时间】:2021-01-30 14:35:57
【问题描述】:

我正在尝试将两个具有自己偏好的现有集合视图单元格添加到我已经存在的表格视图的前两个单元格中,而不会丢失其他表格视图单元格。现在每个视图都可以手动运行。 This is my UI

系列 V1:

import UIKit

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

}

系列 V2:

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

【问题讨论】:

    标签: ios swift uicollectionview tableview uicollectionviewcell


    【解决方案1】:

    对此的好方法,您必须在tableViewHeader 中添加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
    }
    

    【讨论】:

    • 看起来是个好主意。你能解释一下如何将集合视图添加到表格视图的标题
    • 您也可以将它们添加到单行单元格中,如下所示:slicode.com/collectionview-inside-tableview-cell-part-2
    • 看起来更容易将集合视图拖到表格视图的顶部。它工作得很好,但我无法将第二个集合视图拖到顶部。它在原型单元下移动。如何解决?
    • 按照上面的链接并修复 tableView 索引 0 和 1 用于集合视图,其余用于 tableView 行,如果此答案对您有用,您可以将此答案标记为正确,谢谢
    • 我找不到如何设置单元格索引路径
    猜你喜欢
    • 2019-04-18
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多