【问题标题】:Multiple Collection Views with in a viewController with different number of cells具有不同数量单元格的 viewController 中的多个集合视图
【发布时间】:2017-09-01 11:36:29
【问题描述】:

似乎有一个类似的问题here,但它并没有解决我的问题。

我在 viewController 中创建了两个 collectionViews,如下所示:

let y = self.view.frame.height
titleView  = UIView(frame : CGRect(x: 0 , y:  50 , width: self.view.frame.width, height: y - 100 ))

let middle = CGPoint(x: 10, y:   titleView.bounds.height/10)
let frame = CGRect(origin: middle , size: CGSize(width: self.view.frame.width - 20 , height: 70))
let layout = UICollectionViewFlowLayout()
fontCollection = UICollectionView(frame: frame, collectionViewLayout: layout)
fontCollection.dataSource = self
fontCollection.delegate = self
fontCollection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "fontsIdentifier")
fontCollection.backgroundColor = UIColor.white
// Do any additional setup after

colorCollection  = UICollectionView(frame: frame, collectionViewLayout: layout)
//colorCollection.frame.origin.y = fontCollection.frame.origin.y + (2 * fontCollection.frame.height)
colorCollection.dataSource = self
colorCollection.delegate = self
colorCollection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "colorsIdentifier")
colorCollection.backgroundColor = UIColor.white
// Do any additional setup after

这些是我的UICollectionViewDataSource 方法:

func numberOfSections(in collectionView: UICollectionView) -> Int {
    collectionView.reloadData()
    collectionView.collectionViewLayout.invalidateLayout()
    return 1
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if(collectionView == self.colorCollection ){
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "colorsIdentifier", for: indexPath)
        cell.backgroundColor = .darkGray
        return cell
    } else {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "fontsIdentifier", for: indexPath)
        cell.backgroundColor = .gray
        return cell
    }
}

然后我使用segmented control 在两个collectionViews 之间切换。 到目前为止,一切都按预期进行。但是,当我想为 collectionView 分配不同数量的单元格时,我收到此错误:

UICollectionView 接收到带有索引的单元格的布局属性 不存在的路径:{length = 2、路径=0-6}

这是我的numberOfItemsInSection 方法

public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

     if(collectionView == self.colorCollection ){ return 9 }
     else if(collectionView == self.fontCollection ){  return 6  }

     return 0
}

我做错了吗?我错过了什么?

【问题讨论】:

  • 我在此链接中提到了解决方案,但不幸的是,这似乎无法解决我的问题。
  • 发布您重新加载collectionview的代码
  • 我已经更新了我的代码。请看一看。
  • 然后在这里猜测,会不会是分配给两个collectionview的相同布局导致问题?

标签: ios swift uicollectionview


【解决方案1】:

正如@Rajesh73 在评论中指出的那样,问题在于将一个布局分配给collectionViews。所以我给了两个collectionViews不同的布局,它解决了这个问题。

因此替换

  let layout =              UICollectionViewFlowLayout()
  fontCollection  =         UICollectionView(frame: frame, collectionViewLayout: layout)
  colorCollection  =        UICollectionView(frame: frame, collectionViewLayout: layout)

  let fontsLayout =      UICollectionViewFlowLayout()
  fontCollection  =      UICollectionView(frame: frame,collectionViewLayout: fontsLayout)
  let colorsLayout =      UICollectionViewFlowLayout()
 colorsCollection  =     UICollectionView(frame: frame collectionViewLayout: colorsLayout)

解决了这个问题。

【讨论】:

  • 浪费了 2 个小时来解决同样的问题。该死的,从没想过为两个collectionviews共享相同的布局会产生这种问题。救了我的命。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 2017-11-21
  • 1970-01-01
相关资源
最近更新 更多