堆栈 UICollectionView 部分
Posted
技术标签:
【中文标题】堆栈 UICollectionView 部分【英文标题】:Stack UICollectionView sections 【发布时间】:2016-04-20 14:47:51 【问题描述】:我有问题。我正在使用 UICollectionView 显示两个(它们将更多)图像数组,每个数组都有自己的部分。我想实现以下目标:
-----------------------
Section 1: Elements in array no. 1
-----------------------
Section 2: Elements in array no. 2
-----------------------
但到目前为止,我得到了类似的东西
-----------------------
Section 1: Elements in array no. 1 | Elements in array no. 2
-----------------------
我怎样才能告诉 UICollectionView 堆叠这两个部分? 我想过将我现在拥有的集合视图放入另一个集合视图的单元格中,但我不知道该怎么做。 另一种解决方案可能是将它们放入 tableview 的单元格中,但最好是我可以避免这些变通方法。
我不确定如何使用它,但我认为创建自定义布局可能会奏效,即使我需要一些帮助。
另外,我被要求不要使用任何外部库。
顺便说一句,我正在使用 Objective-C 编程。
【问题讨论】:
***.com/questions/20910105/… 谢谢,但这不是我想要的 【参考方案1】:你可以通过简单地实现UICollectionViewDataSource
方法numberOfSectionsInCollectionView
来制作多个部分
class ViewController: UIViewController, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
let HEADER_IDENTIFIER = "header_identifier"
let CELL_IDENTIFIER = "cell_identifier"
lazy var collectionView:UICollectionView =
var cv = UICollectionView(frame: self.view.bounds, collectionViewLayout: self.flowLayout)
cv.delegate = self
cv.dataSource = self
cv.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: self.CELL_IDENTIFIER)
cv.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: self.HEADER_IDENTIFIER)
cv.backgroundColor = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)
return cv
()
lazy var flowLayout:UICollectionViewFlowLayout =
var flow = UICollectionViewFlowLayout()
flow.sectionInset = UIEdgeInsetsMake(2.0, 2.0, 2.0, 2.0)
return flow
()
override func viewDidLoad()
super.viewDidLoad()
self.view.addSubview(self.collectionView)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
// MARK: UICollectionView
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CELL_IDENTIFIER, forIndexPath: indexPath)
cell.backgroundColor = UIColor.blueColor()
return cell
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView
let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: HEADER_IDENTIFIER, forIndexPath: indexPath)
header.backgroundColor = UIColor.redColor()
return header
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
return 3
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
return 2
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
return CGSize(width: 90, height: 90) // The size of one cell
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize
return CGSizeMake(self.view.frame.width, 1) // Header size
【讨论】:
我做到了。我的问题是 2 个部分没有堆叠,而是排成一行。 您使用的是什么布局?你在使用 Flowlayout 吗?你能用你的collectionview检查代表的连接吗? 是的,我正在使用flowlayout,但是我切换到自定义布局没有问题,我只是一个新手。另外,对不起,我没有在我的问题中说清楚,我正在使用objective-c。另外,委托的连接是什么意思? 在情节提要中右键单击您的收藏视图。您将在列表顶部看到数据源和委托。将鼠标悬停在右侧的圆圈上,您会看到+
符号。单击+
标志并将线拖到左侧的ViewController 上,它位于View Controller Scene 下方。对数据源和委托都这样做。
我明白了,我做到了。你能为我提供一个在 Obj-C 中工作的解决方案吗?以上是关于堆栈 UICollectionView 部分的主要内容,如果未能解决你的问题,请参考以下文章
从 uiviewcontroller 操作 uicollectionviewcell 内的 uicollectionview
UICollectionView CompositionalLayout 不调用 UIScrollDelegate
如何将 UICollectionViewCell 从一个 UICollectionView 拖到另一个 UICollectionView?