我有以下代码,它根据屏幕的大小将集合视图放入一个或两个列中:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let size = collectionView.frame.width
if (size > 500) {
return CGSize(width: (size/2) - 8, height: (size/2) - 8)
}
return CGSize(width: size, height: size)
}我想修改这一点,所以高度取决于reuseIdentifier。有两个我用的-设置如下:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let diceRoll = Int(arc4random_uniform(2) + 1)
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("profileViewCell", forIndexPath: indexPath)
if(diceRoll == 1) {
cell = collectionView.dequeueReusableCellWithReuseIdentifier("profileChartViewCell", forIndexPath: indexPath)
}
return cell
}如何获得当前单元格的reuseIndentifier,以便根据单元格的类型更改高度?
发布于 2015-12-02 11:37:21
reuseIdentifier是UICollectionViewReusableView的一个属性,它是UICollectionViewCell的基类。因此,一旦有单元格,就可以随时调用cell.reuseIdentifier。
我不知道你对“当前细胞”的概念是什么。您可以使用collectionView.cellForItemAtIndexPath()在索引路径上询问给定单元格的集合视图,并且可以通过实现委托方法collectionView:didSelectItemAtIndexPath:来跟踪当前选择的单元格,然后保存当前选定的indexPath。
另外(我建议的是)要么是UICollectionViewCell子类并让子类单元格负责其自身的高度,要么实现一个自定义的UICollectionViewLayout类并在那里处理大小。
如果您实现了自己的单元格子类,请确保在自定义单元格上调用registerClass:forCellWithReuseIdentifier:,以便UICollectionView知道如何正确创建它。
https://stackoverflow.com/questions/34040575
复制相似问题