2016-11-18 109 views
0

我有一個自定義UICollectionViewFlowLayout的collectionView。 最後一個collectionview單元格包含一個UITextField。 的細胞根據文本框的寬度resizibleUICollectionView自動佈局奇怪的行爲

- (IBAction)textFieldDidChange:(UITextField*)textField { 

     [_collectionView.collectionViewLayout invalidateLayout]; 

} 

這是我的自定義佈局的代碼

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { 

NSArray* attributesForElementsInRect = [super layoutAttributesForElementsInRect:rect]; 
NSMutableArray* newAttributesForElementsInRect = [[NSMutableArray alloc] init]; 

// use a value to keep track of left margin 
CGFloat leftMargin = 0.0; 

for (id attributes in attributesForElementsInRect) { 
    UICollectionViewLayoutAttributes* refAttributes = attributes; 
    // assign value if next row 
    if (refAttributes.frame.origin.x == self.sectionInset.left) { 
     leftMargin = self.sectionInset.left; 
    } else { 
     // set x position of attributes to current margin 
     CGRect newLeftAlignedFrame = refAttributes.frame; 
     newLeftAlignedFrame.origin.x = leftMargin; 
     refAttributes.frame = newLeftAlignedFrame; 
    } 
    // calculate new value for current margin 
    leftMargin += refAttributes.frame.size.width + 8; 
    [newAttributesForElementsInRect addObject:refAttributes]; 
} 

NSLog(@"newAttributesForElementsInRect : %@", newAttributesForElementsInRect); 
return newAttributesForElementsInRect; 
} 

我想包含文本框的細胞,當它進入到下一行變得足夠大了。 這個問題,layoutAttributesForElementsInRect:(CGRect)rect記錄正確的所需幀,但單元格將不會更新,直到我刷屏幕然後刷回來。 我在那裏可以強制細胞立即得到新的框架。

感謝

+0

你測試使用layoutifneeded後settingLayoutNeeded? – santhosh

+0

是的,我嘗試了[self.view setNeedsLayout]和[self.view layoutIfNeeded]使佈局無效後無效。 奇怪的是,如果內部的文本字段增長,我看到單元格寬度增長。但是當單元格應該到達下一行時會發生問題。它doesen't – zizoodiesel

+0

我注意到,如果我向下滾動contentSize的高度增長,並且該單元格跳轉到下一行,但如果我向上滾動沒有任何反應,直到單元格消散和下一次單元格再次出現在正確的位置 – zizoodiesel

回答

0

所以經過研究兩天我找不到這個問題 我設法在layoutAttributesForElementsInRect設置單元格邊框這樣繞過它一個妥善的解決辦法:

UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:[attributesForElementsInRect indexOfObject:attributes] inSection:0]]; 

if (!CGRectEqualToRect(cell.frame, refAttributes.frame)) 
    cell.frame = refAttributes.frame; 

這是完整的功能

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { 

NSArray* attributesForElementsInRect = [super layoutAttributesForElementsInRect:rect]; 
NSMutableArray* newAttributesForElementsInRect = [[NSMutableArray alloc] init]; 

// use a value to keep track of left margin 
CGFloat leftMargin = 0.0; 

for (id attributes in attributesForElementsInRect) { 
    UICollectionViewLayoutAttributes* refAttributes = attributes; 
    // assign value if next row 
    if (refAttributes.frame.origin.x == self.sectionInset.left) { 
     leftMargin = self.sectionInset.left; 
    } else { 
     // set x position of attributes to current margin 
     CGRect newLeftAlignedFrame = refAttributes.frame; 
     newLeftAlignedFrame.origin.x = leftMargin; 
     refAttributes.frame = newLeftAlignedFrame; 
    } 
    // calculate new value for current margin 
    leftMargin += refAttributes.frame.size.width + 8; 
    [newAttributesForElementsInRect addObject:refAttributes]; 


    UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:[attributesForElementsInRect indexOfObject:attributes] inSection:0]]; 

    if (!CGRectEqualToRect(cell.frame, refAttributes.frame)) 
     cell.frame = refAttributes.frame; 


} 
} 

希望這會幫助某人或有人有更好的解決方案