2016-11-16 135 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 
     } 
相关问题