2016-11-18 106 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; 


} 
} 

希望这会帮助某人或有人有更好的解决方案