因此,这个问题非常简单。我有一个UICollectionViewController
(MyProfile.swift)
标题部分(MyProfileHeader.swift)
。在后者中,我必须UISegmentedControl
返回不同数量的项目和项目collection view cells
(我不想在中初始化后者的类的实例UICollectionViewController
)。这是我的代码MyProfile.swift class
。我尝试在viewForSupplementaryElementOfKind
方法中添加目标以返回不同的查询(有效),但最终我必须访问numberOfItemsInSection
andcellForItemAtIndexPath
方法中的分段控件。的"testObjects"
和"writeObjects"
是array
其经由查询值addTarget
的方法viewForSupplementaryElementOfKind
。我设置的indexPath
,但它returns
的error
原因很明显...我如何访问分段控制?
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
var numberOfItems: Int? = 0
let indexPath = NSIndexPath(forItem: 0, inSection: 0)
let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "header", forIndexPath: indexPath) as! MyProfileHeader
if header.userContent.selectedSegmentIndex == 1 {
numberOfItems = textObjects.count
} else if header.userContent.selectedSegmentIndex == 2 {
numberOfItems = 0
} else {
numberOfItems = photoObjects.count
}
print("F: \(numberOfItems!)")
return numberOfItems!
}
在中为集合视图检索标头时viewForSupplementaryElementOfKind
,可以在中存储对其的弱引用MyProfile
。
class MyProfile: UICollectionViewController {
...
...
weak var header: MyProfileHeader?
...
...
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "header", forIndexPath: indexPath) as! MyProfileHeader
return header
}
然后,您可以从中的任何其他功能访问此功能UICollectionViewController
。
请注意,numberOfItemsInSection
并cellForItemAtIndexPath
已创造了头之前可以叫viewForSupplementaryElementOfKind
,所以当你访问它numberOfItemsInSection
,cellForItemAtIndexPath
或其他任何地方,你应该检查空,然后假定分段控制是默认值(因为这将是因为这是第一次显示视图)。就像是
let selectedSegmentIndex = header?.userContent.selectedSegmentIndex ?? 0 //0 is the default value here
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句