2016-07-22 7 views
1

144個のセルを含むコレクションビューを作成しました。私は彼らに単一の選択のためのセットアップを持っています。私はしばらくの間設定して遊んでいますが、実際にセルを正しく表示する設定には、2つの特定のセルが一貫して間違ったサイズであるというこの問題があります。この問題はiPadプロでテストする場合にのみ発生することに注意してください。彼らは、自分のイメージを他の人に前後に変更するときに複製できない状況下で頻繁にサイズを変更します。私は絶対に必要なときにreloadData()を呼び出すだけで、indexPath関数でリロードされた個々のセルを呼び出そうとします。私はこのことが問題にならないように私が考えることができるすべてについてテストしました。正しい方向のポインターは高く評価されます!ここでコレクションビューのセルのコンテンツサイズが正しくありません

はイメージです。 enter image description here

は、ここに私のコードです:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ChartViewCell 
    cell.setColor(colorArray[indexPath.section][indexPath.row]) 
    cell.overlay?.image = cell.whichColor.toImage() 
    return cell 
} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 24 
} 

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    return 12 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionView, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat { 
    return 0 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { 
    return UIEdgeInsetsMake(0, 0, 0, 0) 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    let screenRect: CGRect = collectionView.bounds 
    let screenWidth: CGFloat = screenRect.size.width 
    let cellWidth: CGFloat = screenWidth/24 
    //Replace the divisor with the column count requirement. Make sure to have it in float. 
    let size: CGSize = CGSizeMake(cellWidth, cellWidth) 
    return size 
} 

答えて

0

私は次の操作を行って問題を修正:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ChartViewCell 
    cell.setColor(colorArray[indexPath.section][indexPath.row]) 
    cell.overlay?.image = cell.whichColor.toImage() 

    let screenRect: CGRect = collectionView.bounds 
    let screenWidth: CGFloat = screenRect.size.width 
    let cellWidth: CGFloat = screenWidth/24 
    cell.singleCellWidthConstraint.constant = cellWidth - 8 
    cell.overlayWidthConstraint.constant = cellWidth - 4 
    return cell 
} 

私はイメージのため幅の制約を加え、細胞のクラスでそれらにコンセントを追加しました。次に、下部と右側の制約を削除し、画像に1:1の制約を追加しました。それはすべてのデバイス上の魅力のように働いた!

1

私は私の以前のプロジェクトと同じ問題が発生しません。私はこのライブラリを使って問題を解決しました:Snapkit。ポッド内のライブラリをインポートし、cellForItemAtIndexPath

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ChartViewCell 

    let width = self.view.bounds.size.width/24 
    cell.overlay.snp_makeConstraints { (make) -> Void in 
     make.width.equalTo(width) 
     make.height.equalTo(width) 
    } 
    cell.setColor(colorArray[indexPath.section][indexPath.row]) 
    cell.overlay?.image = cell.whichColor.toImage() 
    return cell 
} 

だけ置くだけで、トップにそれを実装し、ストーリーボードでImageViewの上の主要な制約。それが役に立てば幸い。

+0

私は答えを感謝しますが、私はこのバグのために私のプロジェクトにサードパーティライブラリを追加したくないです。私はそのような方法で制約を設定しようとし、それが役立つかどうかを見ます。私は現在、それらを細胞のすべての側面に付けています。 – Sethmr

+0

@Sethmr Alright。この問題を解決できるかどうか教えてください。次のプロジェクトに役立ちます。ありがとうございました –

+0

私はそれを修正しました!!!!アイデアのおかげで、ありがとう!私は自分の答えを投げ捨てます。 – Sethmr

関連する問題