uicollectionview Loads wierdly

2019-04-14 20:53发布

I created a collection view with custom collection cell.set with flowlayout as

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(250, 480)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.templateCollectionView setCollectionViewLayout:flowLayout];

I set it horizontal but still a cell is misplaced in figure see att mmp misplaced & it will back in its correct position if we scroll and load it back. Can anyone suggest whats wrong in this ?

collection

2条回答
【Aperson】
2楼-- · 2019-04-14 21:19

It generally happens when we haven't given proper cell spacing. Make sure, while populating cells of uicollectionview, cell spacing should be given. Also go through UICollectionView flowLayout not wrapping cells correctly (iOS) may it help you out.

查看更多
做个烂人
3楼-- · 2019-04-14 21:41

There is a work around on this answer that works for me. Below is the fix I used edited for horizontal scrolling. Basically just filtering out the attributes item that is providing the bad frame down below.

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
     NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
     NSMutableArray *newAttributes = [NSMutableArray arrayWithCapacity:attributes.count];
     for (UICollectionViewLayoutAttributes *attribute in attributes) {
          if (attribute.frame.origin.y + attribute.frame.size.height <=    self.collectionViewContentSize.height) {
               [newAttributes addObject:attribute];
          }
     }
     return newAttributes;
}
查看更多
登录 后发表回答