2016-12-12 5 views
0

У меня есть две ячейки в пределах одного вида коллекции. Оба они имеют разные размеры, но по какой-то причине во время выполнения обе ячейки имеют одинаковое измерение.Две ячейки в одном CollectionView разных размеров

Вот мои настройки:

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 (благодаря cpatmulloy), поместив этот код в cellForItem:

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

Однако вот результат (посмотрите на последнюю ячейку в Tableview):

enter image description here

ответ

1

Попробуйте явно устанавливая высоту ячейки и ширину в функции cellForItemAtIndex Path.

Обязательно установите эти примеры свойств (т.е. bookCellWidth) сами

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 так мне удалось это исправить!

Сначала я добавил UICollectionViewDelegateFlowLayout к cell классу, то:

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 
    } 
} 
Смежные вопросы