uicollectionview Loads wierdly

自古美人都是妖i 提交于 2019-12-06 11:31:48

问题


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 ?


回答1:


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;
}



回答2:


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.



来源:https://stackoverflow.com/questions/15481365/uicollectionview-loads-wierdly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!