UICollectionViewCell在滚动时变得可见

时间:2015-03-20 17:42:58

标签: ios objective-c uicollectionview uicollectionviewcell

我有一个UICollectionView,其中包含七个UICollectionViewCells,这些UICollectionViewCells是使用来自plist文件的信息构建的。当我启动应用程序时,只有四个UICollectionViewCell可见,直到我开始向下滚动 - 这使得紫罗兰UICollectionViewCell可见(见屏幕截图)。我的问题是,如何在应用开始时显示第四个(紫罗兰色)- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath

CategoryCollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:catCellID forIndexPath:indexPath]; NSString *plist = [[NSBundle mainBundle] pathForResource:@"mainCategories" ofType:@"plist"]; NSArray *plistArray = [NSArray arrayWithContentsOfFile:plist]; NSDictionary *cat = [plistArray objectAtIndex:indexPath.item]; CGFloat red = [[cat objectForKey:@"color_red"] doubleValue]; CGFloat green = [[cat objectForKey:@"color_green"] doubleValue]; CGFloat blue = [[cat objectForKey:@"color_blue"] doubleValue]; CGFloat alpha = [[cat objectForKey:@"color_alpha"] doubleValue]; UIColor *color = [UIColor colorWithRed:(red/255.0) green:(green/255.0) blue:(blue/255.0) alpha:1]; [cell setBackgroundColor:color]; cell.label_CategoryName = [cat objectForKey:@"text"]; cell.image_CategoryIcon.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.%@",[cat objectForKey:@"image"], kImageExt]]; return cell; 我正在构建和设置单元格:

{{1}}

enter image description here 谢谢!

1 个答案:

答案 0 :(得分:1)

逐步向下移动集合视图的内容插入,每次1点,直到它开始按照您希望的方式运行。

[_collectionView setContentInset:UIEdgeInsetsMake(0, 10, 0, 0)];
多次触发CellForRowAtIndex以及以下代码...

NSString *plist = [[NSBundle mainBundle] pathForResource:@"mainCategories" ofType:@"plist"];
NSArray *plistArray = [NSArray arrayWithContentsOfFile:plist];
NSDictionary *cat = [plistArray objectAtIndex:indexPath.item];

每次都在内存中构建一个全新的数组实例。这是非常低效的。考虑将数组实例化一次,可能在属性或viewDidLoad中懒惰。