8

समस्या यह है कि संग्रह दृश्य में कॉलम की संख्या घूर्णन पर 7 (वांछित राशि) पर नहीं रहती है। इसे ठीक करने के लिए किस कोड परिवर्तन की आवश्यकता है?अमान्य क्यों है Layout आकार ट्रिगर नहीं कर रहा है UICollectionView मेंForItemAtIndexPath? (कोड संलग्न)

ऐसा लगता है कि कस्टम UICollectionViewFlowLayout से अवैध है Layout आकार दृश्य में ForItemAtIndexPath विधि को ट्रिगर नहीं कर रहा है? कोई विचार? मैं वास्तव में रोटेशन पर आकारफोरमेट इंडेक्सपैथ आकार के माध्यम से कॉलम का आकार बदलना चाहता हूं।

नोट: मैं यहां स्टोरीबोर्ड का उपयोग नहीं कर रहा हूं, बल्कि मेरे पास एक कस्टम दृश्य है जिसमें मैं संग्रह में ड्रॉप करता हूं और अपने स्वयं के संग्रह दृश्य वर्गों का संदर्भ देता हूं।

निम्न कोड ठीक काम करता है, हालांकि घूर्णन पर यह स्तंभों की संख्या को 7 तक नहीं रखता है।

GCCalendar - init coder 
GCCalendar - commonInit 
GCCalendarLayout:invalidateLayout 
GCCalendarLayout:invalidateLayout 
ViewController:viewWillLayoutSubviews 
GCCalendarLayout:invalidateLayout 
GCCalendarLayout:prepareLayout 
sizeForItemAtIndexPath 
. 
. 
sizeForItemAtIndexPath 
minimumInteritemSpacingForSectionAtIndex 
minimumLineSpacingForSectionAtIndex 
GCCalendarLayout:collectionViewContentSize 
GCCalendarLayout:layoutAttributesForElementsInRect 
GCCalendarLayout:collectionViewContentSize 
ViewController:viewWillLayoutSubviews 
GCCalendarCell:drawRect 
. 
. 
GCCalendarCell:drawRect 

हालांकि उसके बाद स्क्रीन मैं निम्न देखें घूर्णन: कोड शुरू में कंसोल आउटपुट शो चल रहा है पर

ViewController:viewWillLayoutSubviews 
GCCalendarLayout:shouldInvalidateLayoutForBoundsChange 
GCCalendarLayout:invalidateLayout 
GCCalendarLayout:prepareLayout 
GCCalendarLayout:collectionViewContentSize 
GCCalendarLayout:layoutAttributesForElementsInRect 
GCCalendarLayout:collectionViewContentSize 

तो इस मुद्दे को कभी नहीं बुलाया गया "sizeForItemAtIndexPath" है ????

रोटेशन पर आउटपुट कोड

नोट: viewWillLayoutSubviews GCCalendarLayout: shouldInvalidateLayoutForBoundsChange GCCalendarLayout:

invalidateLayout "sizeForItemAtIndexPath" यहाँ तक कि "invalidateLayout" हालांकि ट्रिगर नहीं है

ViewController है

** मेरा कस्टम कि घरों संग्रह देखें देखें **

import UIKit 

@IBDesignable class GCCalendarView: UIView { 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     commonInit() 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     commonInit() 
    } 



    // Private 

    private func commonInit() { 
     if self.subviews.count == 0 { 
      let bundle = NSBundle(forClass: self.dynamicType) 
      let nib = UINib(nibName: "GCCalendarView", bundle: bundle) 
      let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView 
      view.frame = bounds 
      view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] 
      addSubview(view) 
     } 
    } 

} 

कस्टम संग्रह देखें

import UIKit 

class GCCalendar : UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate { 

    // Init --------------- 

    func commonInit(coder aDecoder: NSCoder) { 
     print("GCCalendar - commonInit") 
     self.registerClass(GCCalendarCell.self, forCellWithReuseIdentifier: "GCCalendarCell") 
     self.dataSource = self 
     self.delegate = self 

     let layout : GCCalendarLayout = GCCalendarLayout(coder: aDecoder)! 
     self.setCollectionViewLayout(layout, animated: false) 

     self.backgroundColor = UIColor.whiteColor() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     print("GCCalendar - init coder") 
     super.init(coder: aDecoder) 
     commonInit(coder: aDecoder) 
    } 


    // UICollectionViewDelegateFlowLayout ------------ 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
     print("sizeForItemAtIndexPath") 
     let w : CGFloat = floor(self.frame.size.width/7) 
     return CGSize(width: w, height: w) 
    } 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> 
     CGFloat { 
     print("minimumInteritemSpacingForSectionAtIndex") 
     return 0.0 
    } 

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


    // UICollectionViewDataSource ------------------- 

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 21 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("GCCalendarCell", forIndexPath: indexPath) as? GCCalendarCell 


     return cell! 
    } 
} 

** कस्टम CollectionView सेल **

import UIKit 

class GCCalendarCell: UICollectionViewCell { 

    @IBOutlet weak var title : UITextField! 
    @IBOutlet weak var date: UILabel! 

    required init?(coder aDecoder: NSCoder) { 
     print("GCCalendarCell - init:coder") 
     super.init(coder: aDecoder) 
     commonInit() 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     commonInit() 
    } 

    override func drawRect(rect: CGRect) { 
     print("GCCalendarCell:drawRect") 
     self.layer.borderWidth = 1 
     self.layer.borderColor = UIColor.grayColor().CGColor 
    } 

    // Private 

    private func commonInit() { 
     let bundle = NSBundle(forClass: self.dynamicType) 
     let nib = UINib(nibName: "GCCalendarCell", bundle: bundle) 
     let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView 
     view.frame = bounds 
     view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] 
     addSubview(view) 
    } 

} 

कस्टम लेआउट

import UIKit 

class GCCalendarLayout : UICollectionViewFlowLayout { 
    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { 
     print("GCCalendarLayout:shouldInvalidateLayoutForBoundsChange") 
     return true 
    } 

    override func invalidateLayout() { 
     print("GCCalendarLayout:invalidateLayout") 
     super.invalidateLayout() 

    } 

    override func prepareForCollectionViewUpdates(updateItems: [UICollectionViewUpdateItem]) { 
     print("GCCalendarLayout:prepareForCollectionViewUpdates") 
     super.prepareForCollectionViewUpdates(updateItems) 
    } 

    override func finalizeCollectionViewUpdates() { 
     print("GCCalendarLayout:finalizeCollectionViewUpdates") 
     super.finalizeCollectionViewUpdates() 
    } 


} 
+0

आप स्टोरीबोर्ड –

+0

का उपयोग कर रहे हैं। स्टोरीबोर्ड – Greg

+0

का उपयोग नहीं कर रहा हूं, मैं आपकी मदद कर सकता हूं लेकिन मुझे तेज़ी से पता नहीं है। क्या आप obj-c पढ़ सकते हैं? – Kujey

उत्तर

8

संपादित करें:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) 
{ 
    (self.collectionview.collectionViewLayout).setItemSize(CGSizeMake(floor(size.width/7), floor(size.width/7))) 
    self.collectionview.collectionViewLayout.invalidateLayout() 
} 
+0

का उपयोग नहीं करना मेरे पास यह अधिकार होना चाहिए क्योंकि यह चलते समय आकार को सही तरीके से सेट करता है, यह केवल घूर्णन में बदलता है – Greg

+0

समस्याएं उत्पन्न होती हैं। लेकिन फिर भी, क्या आप अपने कस्टम फ्लोलेआउट के आइटम आकार समारोह में लॉग जोड़ सकते हैं, यह देखने के लिए कि क्या इसे रोटेशन पर कहा जाता है या नहीं। – Kujey

+0

sizeForItemAtIndexPath (यदि आपका मतलब है) को रोटेशन पर नहीं कहा जा रहा है ... यह समस्या – Greg

1

अगर आप स्टोरीबोर्ड सेट का उपयोग कर रहे स्टोरीबोर्ड में CollectionviewFlowLayout कक्षा मैं Bellow

this is FlowLayout

संलग्न

Classname Set here

+0

संख्या। स्टोरीबोर्ड – Greg

0

@ Kujey के समाधान: अपने ViewController में इस प्रयास करें तक पहुंच के बिना UIView पर लागू किया जा सकता है:

func traitCollectionDidChange(previousTraitCollection : UITraitCollection) { 
    super.traitCollectionDidChange(previousTraitCollection) 
    self.collectionView.collectionViewLayout.invalidateLayout() 
} 
+0

@EricAya संपादित किया है, मुझे लगता है कि प्रोग्रामिंग भाषा इस प्रश्न के लिए वास्तव में महत्वपूर्ण नहीं है। – k06a

0

मौजूदा उत्तरों (आईफोन एक्स सिम्युलेटर आईओएस 11.2) से मुझे कुछ भी मदद नहीं मिली।
मैंने पाया कि estimatedSize की बदलती इस समस्या के साथ मेरे लिए मदद करता है:

<...> 
//***new line 
flowLayout.estimatedItemSize = [self getIconSize];//get a new size for items 

[flowLayout invalidateLayout]; 
<...> 
संबंधित मुद्दे