9

Ho creato una sottoclasse di collectionViewFlowLayout. Dopo di che, ho implementato il seguente codice:quando si utilizza la sottoclasse collectionViewFlowLayout Ho un errore strano

override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { 
     let attr = self.layoutAttributesForItemAtIndexPath(itemIndexPath) 
     attr?.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(0.8, 0.8), CGFloat(M_PI)) 
     attr?.center = CGPointMake(CGRectGetMidX(self.collectionView!.bounds), CGRectGetMidY(self.collectionView!.bounds)) 
     return attr 
    } 

Quando si elimina elementi in vista collezione utilizzando performBatchUpdates: il metodo debugger genera questo messaggio di errore. La cancellazione ha effettivamente successo e funziona pienamente, ma sono un po 'confuso riguardo questo output del debugger. Qualcuno può spiegare, per favore, debugger? Non capisco veramente quale codice e dove dovrebbe essere aggiunto.

// MESSAGGIO DI ERRORE

2015-08-02 12:39:42.208 nameOfMyProject[1888:51831] Logging only once for UICollectionViewFlowLayout cache mismatched frame 2015-08-02 12:39:42.209 nameOfMyProject[1888:51831] UICollectionViewFlowLayout has cached frame mismatch for index path {length = 2, path = 0 - 11} - cached value: {{106.13333333333333, 131.13333333333333}, {75.733333333333348, 75.733333333333348}}; expected value: {{192.5, 288}, {94.666666666666671, 94.666666666666671}}

2015-08-02 12:39:42.209 nameOfMyProject[1888:51831] This is likely occurring because the flow layout subclass nameOfMyProject.ShopLayout is modifying attributes returned by UICollectionViewFlowLayout without copying them

2015-08-02 12:39:42.209 nameOfMyProject[1888:51831] Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

+0

ha fatto questo solo inizia a succedere con Xcode 7? Qualcosa di molto simile mi è successo anche dopo l'aggiornamento a Xcode 7 (e l'installazione di iOS9). Sono Obj-C, non veloce, ma l'errore ti sta dicendo che non stai copiando gli attributi prima di cambiarli. Ho provato a fare qualcosa come 'NSArray * allAttributesInArray = [[super layoutAttributesForElementsInRect: rect] copy];' prima di manipolarli, ma sembra che non funzioni neanche. – wanderingme

+0

non ho mai usato la vista collezione prima di xcode 7. Ho finito per usare un semplice flowLayout vecchio senza sottoclassi quindi non posso davvero fornire una risposta – brumbrum

+0

@wanderingme hai scoperto come risolvere questo? Ho avuto lo stesso e nulla ha contribuito a risolverlo ancora. – Solomiya

risposta

9

L'errore si verifica perché si sta manipolando l'attributo senza copiarlo prima. Quindi questo dovrebbe correggere l'errore:

override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { 
    let attr = self.layoutAttributesForItemAtIndexPath(itemIndexPath)?.copy() as! UICollectionViewLayoutAttributes 
    // manipulate the attr 
    return attr 
} 

Quando vi imbattete lo stesso errore in layoutAttributesForElementsInRect(rect: CGRect) dovete copiare ciascuna delle voci nella matrice invece di copiare l'array:

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 
     let attributes = super.layoutAttributesForElementsInRect(rect) 
     var attributesCopy = [UICollectionViewLayoutAttributes]() 
     for itemAttributes in attributes! { 
      let itemAttributesCopy = itemAttributes.copy() as! UICollectionViewLayoutAttributes 
      // manipulate itemAttributesCopy 
      attributesCopy.append(itemAttributesCopy) 
     } 
     return attributesCopy 
    } 
+0

La copia di tutti gli attributi in layoutAttributesForElementsInRect ha risolto gli avvertimenti. –

+0

@Joern, questo non funziona – Ranjit

+0

@Ranjit Hai dato un'occhiata al problema github menzionato nei commenti sopra? (https://github.com/wtmoose/TLLayoutTransitioning/issues/26) – joern