0

Im Demo-Projekt kann ich "Hinzufügen eines neuen Tags" drücken, um ein UICollectionViewCell hinzuzufügen, dessen Label meinen neu eingegebenen Text enthält.Hinzufügen von UICollectionViewCell mit dynamisch geänderter Zellenbreite macht minimumInteritemSpacing keine Wirkung?

Das ist der Zieleffekt:

target effect

Diese aktuelle Situation ist: wrong effect

Im aktuellen Zustand i rekonfigurieren in cellForItemAtIndexPath Funktionszelle UI und Zellgröße in sizeForItemAtIndexPath Funktion ändern. Aber diese Vorgänge scheinen dazu zu führen, dass der minimumInteritemSpacing nicht funktioniert. Ich benutze das UICollectionViewFlowLayout.

Antwort

0

ich glaube, Sie UICollectionViewFlowLayout Unterklasse können, und überschreiben Methode -

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect

#import <UIKit/UIKit.h> 
@interface CustomViewFlowLayout : UICollectionViewFlowLayout 

@property(nonatomic) CGFloat maximumLineSpacing; 

@end 

@implementation CustomlViewFlowLayout 

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { 
    NSArray *answer = [super layoutAttributesForElementsInRect:rect]; 
    for(int i = 1; i < answer.count; ++i) { 
     UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i]; 
     UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1]; 

     NSInteger maximumSpacing = self.maximumLineSpacing; 
     NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame); 

     if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) { 
      CGRect frame = currentLayoutAttributes.frame; 
      frame.origin.x = origin + maximumSpacing; 
      currentLayoutAttributes.frame = frame; 
     } 
     if (currentLayoutAttributes.frame.origin.y != prevLayoutAttributes.frame.origin.y) { 
      CGRect frame = currentLayoutAttributes.frame; 
      frame.origin.x = 0; 
      currentLayoutAttributes.frame = frame; 
     } 
    } 
    return answer; 
} 

@end 

Eigentum maximumLineSpacing verwendet, um den maximalen Zeilenabstand einstellen