Ios UICollectionView动态单元格间距和顺序
我有一个CollectionView单元格,与图中的一样,红色单元格是主单元格,绿色单元格是“0单元格”中的子单元格,顺序不固定。 我的问题是,我不能在单元格之间设置不同的空间,因为当我更改“minimumInteritemSpacing”值时,它将更改所有单元格的空间Ios UICollectionView动态单元格间距和顺序,ios,swift,uicollectionview,uicollectionviewlayout,Ios,Swift,Uicollectionview,Uicollectionviewlayout,我有一个CollectionView单元格,与图中的一样,红色单元格是主单元格,绿色单元格是“0单元格”中的子单元格,顺序不固定。 我的问题是,我不能在单元格之间设置不同的空间,因为当我更改“minimumInteritemSpacing”值时,它将更改所有单元格的空间 我在考虑两种选择: 第一个是创建空单元格来填充这些空间,第二个是使用节,并将每一行放在不同的节中 谁能为我提出一个解决方案吗。以下是我迄今为止取得的成果: 我的收藏视图: lazy var MyCV: UICollection
我在考虑两种选择: 第一个是创建空单元格来填充这些空间,第二个是使用节,并将每一行放在不同的节中 谁能为我提出一个解决方案吗。以下是我迄今为止取得的成果: 我的收藏视图:
lazy var MyCV: UICollectionView = {
let myLayout = UICollectionViewFlowLayout()
myLayout.scrollDirection = .vertical
myLayout.minimumInteritemSpacing = 12
myLayout.minimumLineSpacing = 12
myLayout.sectionInset = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 12)
let MycollectionView = UICollectionView(frame: self.view.frame,collectionViewLayout: myLayout)
MycollectionView.delegate = self
MycollectionView.dataSource = self
MycollectionView.backgroundColor = UIColor(hexString: "#FEFEFE")
MycollectionView.showsVerticalScrollIndicator = false
MycollectionView.isScrollEnabled = false
MycollectionView.contentSize = .zero
return MycollectionView
}()
collectionView CellForItem:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 || indexPath.item == 6 || indexPath.item == 7 || indexPath.item == 8{
let cell = MyCV.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MyCell
cell.backgroundView?.backgroundColor = UIColor(hexString: "#F1F3F4")
cell.titleLabel.text = itemsArea[indexPath.row]
return cell
}
else{
let Subcell = MyCV.dequeueReusableCell(withReuseIdentifier:"SubCell", for: indexPath) as! MySubCell
Subcell.backgroundView?.backgroundColor = UIColor(hexString: "#F1F3F4")
Subcell.titleLabel.text = itemsArea[indexPath.row]
return Subcell
}
}
单元格大小:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 70, height: 30)
}
目前,我用sections选项解决了我的问题,我想要的是安静的,但我仍然想知道是否有更好的解决方案