Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以编程方式创建多个CollectionView_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 以编程方式创建多个CollectionView

Ios 以编程方式创建多个CollectionView,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,在一个ViewController中并排获取两个集合视图时遇到一些问题 lazy var listCV: UICollectionView = { let listLayout = UICollectionViewFlowLayout() listLayout.itemSize = CGSize(width: 300, height: 40) listLayout.minimumLineSpacing = 5 listLayout.sectionHeadersP

在一个ViewController中并排获取两个集合视图时遇到一些问题

lazy var listCV: UICollectionView = {

    let listLayout = UICollectionViewFlowLayout()
    listLayout.itemSize = CGSize(width: 300, height: 40)
    listLayout.minimumLineSpacing = 5
    listLayout.sectionHeadersPinToVisibleBounds = true

    let listFrame = CGRect(x: 0, y: 64, width: 325, height: 500)

    let listView = UICollectionView(frame: listFrame, collectionViewLayout: listLayout)
    listView.delegate = self
    listView.dataSource = self

    listView.backgroundColor = UIColor.cyan

    return listView
}()

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

if collectionView == listCV {
   let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: listHeaderID, for: indexPath) as! HeaderCell
   return header     
    } else { ...
    }
}
我的问题不是构建或显示集合视图。单独地,我可以在屏幕上看到我想要的两个,只是不能同时看到

我已经调试了我的代码,发现在设置“cellForItemAt”时它失败了

我也搜索了论坛,了解到我需要一个if声明来说明

如果collectionView==1,则将单元A出列,否则将单元B出列

不幸的是,当我要编写if语句时,xCode找不到我的集合视图,也就是说,不会自动找到它们以便编译。此外,我的手机还有一个“未解析标识符”

鉴于我以上的努力,我遇到了麻烦,想知道是否有人能为我的问题提供一些线索?谢谢

查看加载代码

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.isNavigationBarHidden = false

    setupCollectionView()   
}
func setupCollectionView() {

    let listLayout = UICollectionViewFlowLayout()
    listLayout.itemSize = CGSize(width: 300, height: 40)
    listLayout.minimumLineSpacing = 5
    listLayout.sectionHeadersPinToVisibleBounds = true
    let listFrame = CGRect(x: 0, y: 64, width: 325, height: 500)
    let listView = UICollectionView(frame: listFrame, collectionViewLayout: listLayout)

    // cell
    listView.register(cellA.self, forCellWithReuseIdentifier: cellAId)

    // header
    listView.register(headerCellA.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerAID)

    listView.delegate = self
    listView.dataSource = self
    listView.backgroundColor = .cyan

    self.view.addSubview(listView)

    let detailLayout = UICollectionViewFlowLayout()
    detailLayout.itemSize = CGSize(width: 300, height: 40)
    detailLayout.minimumLineSpacing = 5
    detailLayout.sectionHeadersPinToVisibleBounds = true
    let detailFrame = CGRect(x: 330, y: 64, width: 500, height: 650)
    let detailView = UICollectionView(frame: detailFrame, collectionViewLayout: detailLayout)

    // cell
    detailView.register(cellB.self, forCellWithReuseIdentifier: cellBID)

    // header
    detailView.register(headerCellB.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerBID)

    detailView.delegate = self
    detailView.dataSource = self
    detailView.backgroundColor = .white

    self.view.addSubview(detailView)
}
集合视图创建代码

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.isNavigationBarHidden = false

    setupCollectionView()   
}
func setupCollectionView() {

    let listLayout = UICollectionViewFlowLayout()
    listLayout.itemSize = CGSize(width: 300, height: 40)
    listLayout.minimumLineSpacing = 5
    listLayout.sectionHeadersPinToVisibleBounds = true
    let listFrame = CGRect(x: 0, y: 64, width: 325, height: 500)
    let listView = UICollectionView(frame: listFrame, collectionViewLayout: listLayout)

    // cell
    listView.register(cellA.self, forCellWithReuseIdentifier: cellAId)

    // header
    listView.register(headerCellA.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerAID)

    listView.delegate = self
    listView.dataSource = self
    listView.backgroundColor = .cyan

    self.view.addSubview(listView)

    let detailLayout = UICollectionViewFlowLayout()
    detailLayout.itemSize = CGSize(width: 300, height: 40)
    detailLayout.minimumLineSpacing = 5
    detailLayout.sectionHeadersPinToVisibleBounds = true
    let detailFrame = CGRect(x: 330, y: 64, width: 500, height: 650)
    let detailView = UICollectionView(frame: detailFrame, collectionViewLayout: detailLayout)

    // cell
    detailView.register(cellB.self, forCellWithReuseIdentifier: cellBID)

    // header
    detailView.register(headerCellB.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerBID)

    detailView.delegate = self
    detailView.dataSource = self
    detailView.backgroundColor = .white

    self.view.addSubview(detailView)
}
cellForItemAt代码-无法使我的if语句工作

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

if collectionView == detailView {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellAID, for: indexPath) as! ListCell
} else {  
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellBID, for: indexPath) as! DetailCell
    }
    return cell
}

以下是实现这一目标的方法。试着像这样对你的方法进行降级

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

if let collectionView = detailView as? CollectionViewB {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellAID, for: indexPath) as! ListCell
} else {  
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellBID, for: indexPath) as! DetailCell
    }
    return cell
}
仅当您有两个UICollectionView插座时,上述功能才起作用

  • 另一个调整是可以在xib上拖放两个UIView 然后加载两个UIViewController类,每个类具有单独的 UICollectionView对象。不管你找到什么最好的方法,先实现它。我只是想让大家了解一下

    • 不幸的是,当我编写if语句时,xCode找不到我的集合视图,也就是说,不会自动找到它们以便编译。此外,我的手机还有一个“未解析标识符”。


      这是因为您尚未全局定义集合视图。您已经在本地
      setupCollectionView
      中定义了它们,这就是您无法在自动完成中找到它的原因

      感谢所有发表评论的人。通过将我的简历创建为全局惰性var闭包,这一点已经得到了解决

      这允许我在CV数据源方法中访问它们,并为我的cellForItemAt指定不同的引用,从而允许在一个viewController中显示两个CV

      lazy var listCV: UICollectionView = {
      
          let listLayout = UICollectionViewFlowLayout()
          listLayout.itemSize = CGSize(width: 300, height: 40)
          listLayout.minimumLineSpacing = 5
          listLayout.sectionHeadersPinToVisibleBounds = true
      
          let listFrame = CGRect(x: 0, y: 64, width: 325, height: 500)
      
          let listView = UICollectionView(frame: listFrame, collectionViewLayout: listLayout)
          listView.delegate = self
          listView.dataSource = self
      
          listView.backgroundColor = UIColor.cyan
      
          return listView
      }()
      
      func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
      
      if collectionView == listCV {
         let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: listHeaderID, for: indexPath) as! HeaderCell
         return header     
          } else { ...
          }
      }
      

      您需要在属性而不是局部变量中存储对集合视图的引用。这样,您就可以在数据源中进行比较method@Paulw11感谢您的评论,这有助于重新评估我的方法否,集合视图不是全局定义的。它们是在
      setupCollectionView
      @luckyShubhra中本地定义的,感谢您的输入。我重新评估了我是怎么做的。