2016-11-16 171 views
0

我正在使用UICollectionView,我正在嘗試使用UICollectionViewLayout方法來調整單元的大小,但它們永遠不會被調用。三是方法: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委託

請嘗試:

self.collectionView.delegate = selfviewDidLoad()方法

其實從UICollectionViewDelegateUICollectionViewDelegateFlowLayout繼承。所以設置委託就足夠了。

快樂編碼!

+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 
     } 
相關問題