2016-11-16 21 views
0

私はUICollectionViewを使用していますが、UICollectionViewLayoutメソッドを使用してセルのサイズを調整しようとしていますが、呼び出されることはありません。 3の方法は以下のとおりです。UICollectionViewLayoutメソッドが呼び出されない

sizeForItemAtIndexPathminimumInterimItemSpacingForSectionAtIndex、およびminimumLineSpacingForSectionAtIndex

これらの3つの方法が呼び出されることはありませんされていると私はどこから/それらを呼び出すするかどうかはわかりませんされています。以下スウィフト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デリゲートに

を設定していないと考えてみてください:viewDidLoad()方法

self.collectionView.delegate = selfUICollectionViewDelegateから実際にUICollectionViewDelegateFlowLayout継承。だからデリゲートの設定で十分です。

ハッピーコーディング!

+0

を第一に、私はこの

class CollectionVC: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

に私のクラスのラインを変更しなければならなかったし、私はスウィフト3構文にメソッドの宣言を変更する必要がありました動作していません – MikeG

+0

あなたはどのように設定しますか?ストーリーボードで?ストーリーボードの – iProgrammer

+0

はい。そして、プログラムで行を追加しても役立たない – MikeG

0

私はXinatanilの助けを借りて問題を解決できました。私はすでにcollectionViewデリゲートを設定しているし、まだそれがある...

 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 
     } 
関連する問題