UICollectionView autolayout奇怪的行为

时间:2016-11-18 01:50:42

标签: ios objective-c uicollectionview

我有一个带有自定义UICollectionViewFlowLayout的collectionView。 最后一个collectionview单元包含一个UITextField。 根据文本域的宽度

,单元格可以被删除
- (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 的问题记录了正确的所需帧,但是单元格不会更新,直到我在屏幕上滑动然后再向后滑动。 我可以做些什么来迫使细胞立即获得它的新框架。

谢谢

1 个答案:

答案 0 :(得分: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;


}
}

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