2016-11-16 4 views
0

Я работаю с UICollectionView, и я пытаюсь использовать методы UICollectionViewLayout для настройки размера ячейки, однако они никогда не вызываются. Способ 3 являются:Методы UICollectionViewLayout никогда не вызываются

sizeForItemAtIndexPath, minimumInterimItemSpacingForSectionAtIndex и minimumLineSpacingForSectionAtIndex

эти 3 метода никогда не вызывается, и я не уверен, где/как назвать их. ниже мой код в Swift 3

import Foundation 
import UIKit 

class CollectionVC: UICollectionViewController { 


     var Sections = [SectionService]() 



     override func viewDidLoad() { 
      createServiceSections() 
     } 


     func createServiceSections(){ 

      ServiceSections.append(ServiceSection(headerTitle: "1", items: 2, headerImage: UIImage(named: "Default"), stats: nil)) 

      ServiceSections.append(ServiceSection(headerTitle: "2", items: 3, headerImage: UIImage(named: "Default"), stats: nil)) 

      ServiceSections.append(ServiceSection(headerTitle: "3", items: 3, headerImage: UIImage(named: "Default"), stats: nil)) 

      ServiceSections.append(ServiceSection(headerTitle: "4", items: 5, headerImage: UIImage(named: "Default"), stats: nil)) 
     } 


     override func numberOfSections(in collectionView: UICollectionView) -> Int { 
      return ServiceSections.count 
     } 


     override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

      return ServiceSections[indexPath.section].items.count 

     } 

     override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

      let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "servicecell", for: indexPath) as! ServicesCellView 

      cell1.serviceLabel.text = "test" 
      cell1.serviceImage.image = ServiceSections[indexPath.section].headerImage 


      return cell1 
     } 





     func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

      print("size for item at indexPath") 
      var cellSize: CGSize 
      let collectionViewWidth = self.collectionView!.bounds.size.width 

      cellSize = CGSize(width: (collectionViewWidth * 0.48), height: (collectionViewWidth * 0.48)) 

      return cellSize 

     } 

     func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInterItemSpacingForSectionAtIndex section: Int) -> CGFloat { 
      print("min spacing section") 
      return 1 

     } 

     func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat { 
      print("min line spacing section") 
      return 1 
     } 





} 

ответ

0

Я думаю, вы не установили CollectionView делегат

Пожалуйста, попробуйте:

self.collectionView.delegate = self в viewDidLoad() метод

UICollectionViewDelegateFlowLayout На самом деле наследуется от UICollectionViewDelegate. Поэтому установить делегата будет достаточно.

Счастливое кодирование!

+0

Я уже установить делегат CollectionView и все это не работает – MikeG

+0

как вы устанавливаете? В раскадровке? – iProgrammer

+0

есть в раскадровке. И даже добавление линии программно не помогает – MikeG

0

С помощью Xinatanil я смог исправить проблему. Во-первых, я должен был изменить свою классовую линию к этому

class CollectionVC: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

, а затем я должен был изменить заявления методы Свифт 3 синтаксисом ...

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

      var cellSize: CGSize 
      let collectionViewWidth = self.collectionView!.bounds.size.width 

      cellSize = CGSize(width: (collectionViewWidth * 0.48), height: (collectionViewWidth * 0.48)) 

      return cellSize 

     } 

     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 
      return 1 
     } 


     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
      return 1 
     }