0

画面のサイズに応じて、コレクション ビューを 1 つまたは 2 つの列に配置する次のコードがあります。

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に依存します。私が使用するものは2つあります-次のように設定します:

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 を取得するにはどうすればよいですか?

4

1 に答える 1

0

reuseIdentifierは、の基本クラスUICollectionViewReusableViewであるのプロパティです。UICollectionViewCellしたがってcell.reuseIdentifier、セルがあればいつでも電話をかけることができます。

あなたの「現在のセル」の概念が何であるかわかりません。を使用して、コレクション ビューにインデックス パスの特定のセルを要求できますcollectionView.cellForItemAtIndexPath()。デリゲート メソッドcollectionView:didSelectItemAtIndexPath:を実装し、現在選択されている indexPath を保存することで、現在選択されているセルを追跡できます。

代わりに(そして私が推奨するのは)、サブクラスUICollectionViewCell化してサブクラス化されたセルにそれ自体の高さを持たせるか、カスタムUICollectionViewLayoutクラスを実装してそこにサイズを処理することです。

独自のセル サブクラスを実装する場合は、必ずregisterClass:forCellWithReuseIdentifier:カスタム セルを呼び出して、がセルUICollectionViewを適切に作成する方法を認識できるようにしてください。

于 2015-12-02T11:37:21.153 に答える