嘿伙计有人曾经遇到一个问题,UICollectionViewCell
在滚动UICollectionView
时消失了。我实现了一个自定义UICollectionViewLayout
,一切似乎都运行良好。但是当我滚动(快速)时,有时单元格会消失,这主要发生在我在UICollectionView
的顶部或底部边缘滚动时。
我想指出的另一个问题是当UICollectionDataCell被用来呈现图像时UICollectioView的滚动性能,如下所示。如何改进这种滚动性能?
CustomUICollectionLayoutFile
@implementation MellowFeedViewLayout
- (void)prepareLayout {
_cellAttributes = [[NSMutableArray alloc] init];
float firstColX = 10.0f;
float secondColX = firstColX + ((MellowFeedViewCellData *)self.data[0]).size.width + _spacingBetweenCols;
float firstColY = 10.0f;
float secondColY = 10.0f;
for (int i=0; i<[self.data count]; i++) {
MellowFeedViewCellData *data = self.data[i];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
if (i % 2 == 1) {
// First Col
attributes.frame = CGRectMake(firstColX, firstColY, data.size.width, data.size.height);
firstColY += data.size.height + 10.0f;
} else {
// Second Col
attributes.frame = CGRectMake(secondColX, secondColY, data.size.width, data.size.height);
secondColY += data.size.height + 10.0f;
}
[_cellAttributes addObject:attributes];
}
_maxHeight = MAX(firstColY, secondColY);
}
- (CGSize)collectionViewContentSize {
return CGSizeMake(UIScreen.mainScreen.bounds.size.width, _maxHeight);
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
return _cellAttributes;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
return _cellAttributes[indexPath.item];
}
CustomUICollectionManagerFile
@implementation MellowFeedViewController
- (void)viewDidLoad
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MellowFeedViewLayout *layout = [[MellowFeedViewLayout alloc] init];
layout.spacingBetweenCols = 10.0f;
layout.data = self.data;
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[_collectionView setShowsHorizontalScrollIndicator:NO];
[_collectionView setShowsVerticalScrollIndicator:NO];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[_collectionView setBackgroundColor:[UIColor colorWithRed:232.0/255.0f green:232.0/255.0f blue:232.0/255.0f alpha:1.0f]];
[self.view addSubview:_collectionView];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - CHTCollectionViewDelegateWaterfallLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
MellowFeedViewCellData *data = (MellowFeedViewCellData *)self.data[indexPath.item];
return CGSizeMake(data.size.width, data.size.height);
}
//- (UIEdgeInsets)collectionView:
//(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
// return _borderMargin;
//}
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*) minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
// return UIEdgeInsetsMake(0, 0, 0, 0);
//}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.data count];
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
//TODO::Make my own layout http://www.rahuljiresal.com/2014/03/understanding-chtcollectionviewwaterfalllayout/
MellowFeedViewCellData *data = (MellowFeedViewCellData *)self.data[indexPath.item];
[cell setBackgroundView:data.cellView];
return cell;
}
@end
数据文件
@implementation MellowFeedViewCellData
- (instancetype)initWithImage:(NSString *)feedImage {
self = [super init];
if (!self) return nil;
_feedImage = feedImage;
int rndIndex = arc4random() % 2;
if (rndIndex == 0) {
_size = CGSizeMake(145.0f, 230.0f);
} else if(rndIndex == 1) {
_size = CGSizeMake(145.0f, 150.0f);
}
// Set Cell View
_cellView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _size.width, _size.height)];
// Adds Shadow
[_cellView.layer setShadowColor:[UIColor blackColor].CGColor];
[_cellView.layer setShadowOpacity:0.5];
[_cellView.layer setShadowRadius:1.0];
[_cellView.layer setShadowOffset:CGSizeMake(1.0, 1.0)];
// Add rounder corner container
UIView *cellViewContentContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _size.width, _size.height)];
[cellViewContentContainer setBackgroundColor:[UIColor whiteColor]];
cellViewContentContainer.layer.cornerRadius = 5;
cellViewContentContainer.layer.masksToBounds = YES;
[_cellView addSubview:cellViewContentContainer];
// Set bgImage
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _size.width, _size.height)];
bgImageView.contentMode = UIViewContentModeScaleAspectFill;
[bgImageView setImage:[UIImage imageNamed:_feedImage]];
[bgImageView setClipsToBounds:YES];
[cellViewContentContainer addSubview:bgImageView];
// Set title
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, _size.height - 60.0f, _size.width, 60.0f)];
[title setText:@"Breaking news, very important."];
[title setNumberOfLines:2];
[title setTextColor:[UIColor whiteColor]];
[title setFont:[UIFont systemFontOfSize:13]];
[title setTextAlignment:NSTextAlignmentCenter];
[title setBackgroundColor:[UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.5f]];
[cellViewContentContainer addSubview:title];
return self;
}