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

测试仪

我想合并分别使用两个不同单元格(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)合并。像这样。

在此处输入图片说明

我找不到实现此目标的解决方案。任何帮助将不胜感激。

瓦瓦瓦玛

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

  1. aCells添加数量属性bCells这比将魔术数字 31代码放入代码中更好,因为它可以记录数字代表的含义。如果您需要更改它们,可以一次更改它们。
  2. numberOfSections回来1
  3. 更改numberOfItemsInSection以返回A单元数目和B单元数目之和:return aCells + bCells
  4. cellForItemAt,比较indexPath.itemaCells找出当切换到B细胞。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

iOS-合并两个不同大小的图像

iOS中视图中的不同部分

iOS如何通过PureLayout将两个视图居中

如何在Scenekit- iOS中检测两个不同.scn文件的节点之间的冲突?

合并两个iOS应用

IOS UICollectionView 在使用两个集合视图时抛出断言

在两个表格视图中加载数据+分段控件_ iOS

如何在iOS 8中关闭两个UIViewController?

如何在iOS中交换两个UIWebView

如何在具有两个故事板 iOS Swift 的项目中初始化视图控制器

一键操作推两个不同的视图控制器-iOS

如何在iOS Swift项目中使用两个不同的GoogleService-info.plist文件开发产品?

如何使用 Autolayout iOS 在两个视图之上设置视图

如何处理两个容器视图中的两个集合视图

如何在Django视图中合并两个或多个查询集?

iOS:如何同步两个UIScrollview

如何从Mongodb中的两个不同集合进行合并?

如何使用幽灵合并两个不同的集合?

iOS Swift:如何使用UIBezierPath将两个形状合并为纯色形状?

在laravel中如何在单个视图中显示来自两个不同模型的值

iOS自动版式:等宽且等距的两个视图

iOS Swift:如何在同一项目上为不同国家/地区构建两个不同的.ipa

如何在larave中合并两个相同的集合

如何在VBA中合并两个集合?

如何在 cypher 中合并两个集合

如何在iOS的视图中访问子类的成员?

iOS SwiftUI:如何在视图中检测UITapGesture的位置?

如何在拆分视图中检测iOS应用

如何在一个tableView中使用两个不同的自定义单元格?使用iOS 7的Storyboard