Ios 我用两行单元格实现UICollectionView,我需要两行单元格可以同时水平滚动
我有一个水平滚动的UICollectionView。我需要将所有集合视图单元格分为两行,两行都应水平滚动。布局如屏幕截图所示 如上面的屏幕截图所示,我将要构建两行,它们将水平滚动,也就是说,两行将在同一方向上一起滚动 我之前考虑过在scroll视图中使用节,但是滚动可能是独立的,因此,我希望找到更好的解决方案 我在这里查看了这个链接: 此链接使用tableview保存多个集合视图。尽管这个解决方案看起来不错,但我真的不确定它是否适合我,我想知道是否有更好的替代方案。 我查看了其他关于相同的堆栈溢出的帖子(虽然不记得是哪个),但它们没有多大帮助。 现在,通常情况下,集合视图中有两列,我们可以垂直滚动。有没有办法在UICollectionView中同时水平滚动两行,而不是这种行为Ios 我用两行单元格实现UICollectionView,我需要两行单元格可以同时水平滚动,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我有一个水平滚动的UICollectionView。我需要将所有集合视图单元格分为两行,两行都应水平滚动。布局如屏幕截图所示 如上面的屏幕截图所示,我将要构建两行,它们将水平滚动,也就是说,两行将在同一方向上一起滚动 我之前考虑过在scroll视图中使用节,但是滚动可能是独立的,因此,我希望找到更好的解决方案 我在这里查看了这个链接: 此链接使用tableview保存多个集合视图。尽管这个解决方案看起来不错,但我真的不确定它是否适合我,我想知道是否有更好的替代方案。 我查看了其他关于相同的堆
我应该考虑使用两个集合视图,并有一些逻辑来约束两个视图的滚动吗?(我不希望只有两个UICollectionView来解决这个问题) 而且,我是通过纯代码来实现的。没有使用故事板或xib文件
你能试试下面的代码吗 我想我能做到这一点class CollectionViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
setUpCollectionView()
}
fileprivate func setUpCollectionView() {
let collectionFlowLayout = UICollectionViewFlowLayout()
collectionFlowLayout.scrollDirection = .horizontal
collectionFlowLayout.itemSize = CGSize(width: 145, height: 145)
collectionView.setCollectionViewLayout(collectionFlowLayout, animated: true)
collectionView.delegate = self
collectionView.dataSource = self
}
}
extension CollectionViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCollectionViewCell", for: indexPath)
if indexPath.row % 2 == 0 {
cell.contentView.backgroundColor = UIColor.red
} else {
cell.contentView.backgroundColor = UIColor.green
}
return cell
}
}
注意:我已将collectionView高度设置为300
输出
使用2
collectionView
let CellId1 = "CellId1"
lazy var collectionViewOne: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
let width = collectionView.frame.width
let collectionViewOne = UICollectionView(frame: CGRect(x: 0, y: 100, width: width, height: 100), collectionViewLayout: layout)
collectionViewOne.showsHorizontalScrollIndicator = false
collectionViewOne.backgroundColor = .red
return collectionViewOne
}()
let CellId2 = "CellId2"
lazy var collectionViewTwo: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
let collectionViewTwo = UICollectionView(frame: CGRect(x: 0, y: 250, width: width, height: 100), collectionViewLayout: layout)
collectionViewTwo.backgroundColor = .blue
return collectionViewTwo
}()
然后获取numberOfItem和cellForRow:
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.collectionViewOne {
return 10
} else if collectionView == self.collectionViewTwo {
return 20
}
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.collectionViewOne {
let Cell1 = collectionViewOne.dequeueReusableCell(withReuseIdentifier: CellId1, for: indexPath)
Cell1.backgroundColor = .green
return Cell1
} else if collectionView == self.collectionViewTwo {
let Cell2 = collectionViewTwo.dequeueReusableCell(withReuseIdentifier: CellId2, for: indexPath )
Cell2.backgroundColor = .purple
return Cell2
}
}
别忘了在viewDidLoad()中注册collectionView
如果可以垂直分组单元,请创建一个具有两个行视图的单元,使其看起来像两个单元。同时禁用UICollectionViewCell选择并添加手势手柄我不遵循。你的意思是说我应该在UICollectionViewCell中有两个类似于单元格的UIView,一个在另一个的下面吗?是的,如果你可以垂直分组视图的话,对我来说,这似乎是一种攻击。但有总比没有好@赛林老板:谢谢你的建议。如果有更好的解决方案,那就太好了,不过,这对我来说暂时是可行的。
collectionViewOne.delegate = self
collectionViewOne.dataSource = self
collectionViewTwo.delegate = self
collectionViewTwo.dataSource = self
collectionViewOne.register(Cell1.self, forCellWithReuseIdentifier: storyCell)
collectionViewTwo.register(Cell2.self, forCellWithReuseIdentifier: cardCell)