2016-12-12 6 views
0

1つのコレクションビュー内に2つのセルがあります。それらはどちらも異なるディメンションを持ちますが、何らかの理由で実行時に両方のセルが同じディメンションを持つように見えます。ここで異なる次元の単一CollectionView内の2つのセル

は私の設定です:

internal func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    if cellType == .books { 
     return books.count 
    } else if cellType == .spotlights { 
     return spotlights.count 
    } else { 
     return 0 
    } 
} 

internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    if cellType == .books { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "booksCollectionCell", for: indexPath) as! BooksCollectionCell 
     let bookTitle = books[indexPath.item].title 
     let authors = books[indexPath.item].authors 
     cell.configureCell(title: bookTitle, authorNames: authors) 
     return cell 
    } else { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "spotlightsCollectionCell", for: indexPath) as! spotlightsCollectionViewCell 
     cell.configureCell(image: #imageLiteral(resourceName: "testImage")) 
     return cell 
    } 
} 

は、ここに私のストーリーボードのスクリーンショットです:

enter image description here

、これはcollectionViewのセットアップです:

enter image description here

編集:だから私は、管理対象somwhを取得するERE cellForItemにこのコードを入れて(cpatmulloyのおかげ):

cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y, width: 180.0, height: 130.0) 

は、しかし、ここでの結果は、(テーブルビューの最後のセルを見て)です:

enter image description here

答えて

1

セルの高さを設定明示的に試してみてくださいcellForItemAtIndex Path関数の幅を指定します。

自分はので、私はそれを修正するために管理

internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     if cellType == .books { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "booksCollectionCell", for: indexPath) as! BooksCollectionCell 
      let bookTitle = books[indexPath.item].title 
      let authors = books[indexPath.item].authors 
      cell.configureCell(title: bookTitle, authorNames: authors) 

      cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y, width: bookCellWidth, height: bookCellHeight) 

      return cell 
     } else { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "spotlightsCollectionCell", for: indexPath) as! spotlightsCollectionViewCell 
      cell.configureCell(image: #imageLiteral(resourceName: "testImage")) 

      cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y, width: spotlightCellWidth, height: spotlightCellHeight) 

      return cell 
     } 
    } 
+0

をこれらが得る専用のプロパティです... –

+0

私が行った編集内容を確認してください^ – cpatmulloy

+0

は違いのビットを作ったが、今セルが重複しています –

0

OK(すなわちbookCellWidth)これらの例のプロパティを設定してください!

まず私はその後、cellクラスにUICollectionViewDelegateFlowLayoutを追加しました:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    if cellType == .spotlights { 
     return CGSize(width: 250.0, height: 180.0) 
    } else { 
     return (collectionViewLayout as! UICollectionViewFlowLayout).itemSize 
    } 
} 
関連する問題