ios-如何在集合视图中合并两个不同的部分

ios-如何在集合视图中合并两个不同的部分,ios,swift,cells,collectionview,Ios,Swift,Cells,Collectionview,我想合并两个分别使用两个不同单元格(cellA和cellB)的不同部分。使用下面的代码,我可以得到一个包含两个部分的集合视图 import UIKit class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{ @IBOutlet var testCollectionView: UICollectionView! override func

我想合并两个分别使用两个不同单元格(cellA和cellB)的不同部分。使用下面的代码,我可以得到一个包含两个部分的集合视图

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{

    @IBOutlet var testCollectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.testCollectionView.delegate = self
        self.testCollectionView.dataSource = self
    }
   
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return 3
        } else {
            return 1
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "taskCell", for: indexPath) as! CellA
            cell.layer.cornerRadius = 2
            cell.layer.shadowColor = UIColor(red:0.82, green:0.82, blue:0.82, alpha:1.0).cgColor
            cell.layer.shadowOffset = CGSize.zero
            cell.layer.shadowOpacity = 1
            cell.layer.shadowRadius = 4
            cell.layer.masksToBounds = false
            return cell
        } else {
            let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "addNewCell", for: indexPath) as! CellB
            return cell2
        }
    }
}

extension ViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let width = testCollectionView.bounds.width/2 - 30
        let height = width
        
        return CGSize(width: width, height: height)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(30, 20, 10, 20)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 20
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.row)
    }
}
输出结果如下所示

现在,我想将第1节(CellB)与第0节(CellA)合并。像这样


我找不到实现这一目标的解决方案。如有任何帮助,我们将不胜感激。

您可以通过以下更改在同一部分中显示两种单元格类型:

  • aCells
    bCells
    的数量添加属性。这比在代码中放入神奇的数字
    3
    1
    要好,因为它记录了数字所代表的内容。如果你需要改变它们,你可以在一个地方改变它们
  • 更改
    numberOfSections
    以返回
    1
  • 更改
    numberOfItemsInSection
    以返回A单元格数和B单元格数之和:
    返回aCells+bCells
  • cellForItemAt
    中,将
    indexath.item
    aCells
    进行比较,以确定何时切换到B单元格

  • var aCells=3
    var bCells=1
    func numberOfSections(在collectionView:UICollectionView中)->Int{
    返回1
    }
    func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
    返回aCells+b单元格
    }
    func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
    如果indexath.item
    我相信您只需从第1节中删除
    单元格B
    ,然后在第0节中使用相同的数据创建一个新的单元格。您所说的是将单元格B移动到a节的动画,还是将四个单元格移动到初始状态的一节?在
    cellForItem
    方法中,计算出需要有多少个单元格a(基于您的数据),然后,当
    indexPath.item
    表示下一个单元格时,将其设为单元格B。在
    numberOfItemsInSection
    方法中执行类似操作,以获得比单元格a的数量多一个单元格。
    var aCells = 3
    var bCells = 1
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return aCells + bCells
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.item < aCells {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "taskCell", for: indexPath) as! CellA
            cell.layer.cornerRadius = 2
            cell.layer.shadowColor = UIColor(red:0.82, green:0.82, blue:0.82, alpha:1.0).cgColor
            cell.layer.shadowOffset = CGSize.zero
            cell.layer.shadowOpacity = 1
            cell.layer.shadowRadius = 4
            cell.layer.masksToBounds = false
            return cell
        } else {
            let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "addNewCell", for: indexPath) as! CellB
            return cell2
        }
    }