UICollectionView只显示第一个项目
我正在做一个类似于video专辑的项目。 在这我使用UICollectionView
来显示这些video的拇指图像。 最糟糕的是我不应该使用storyboard或xib文件。 我试图以编程方式做到这一点。 我目前正在处理这个代码:
- (void)viewDidLoad { i = 0; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; [layout setItemSize:CGSizeMake(self.view.frame.size.width/2.5,self.view.frame.size.width/2.5)]; collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout]; [collectionView setDataSource:self]; [collectionView setDelegate:self]; collectionView.backgroundColor = [UIColor whiteColor]; [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"]; [self.view addSubview:collectionView]; [super viewDidLoad]; }
我已经给numberOfSectionsInCollectionView
返回1和[myArray count]
返回numberOfItemsInSection
。
-(UICollectionViewCell *) collectionView:(UICollectionView *)cV cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [cV dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; cell.backgroundColor = [UIColor blackColor]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3))]; cell.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3))]; imageView.image = [UIImage imageNamed:[storeData objectAtIndex:i]]; [cell addSubview:imageView]; i++; return cell; }
我已经重新检查myArray
的图像。 加载视图时,集合视图仅显示第一个图像。 其他4个单元格是空的。 我的代码有什么问题?
对于遇到这个问题的人来说, frame
是关键。 我遇到了这个,并改变了:
cell.imageView.frame = cell.frame;
成
cell.imageView.frame = cell.bounds;
操作正在使用:
cell.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3))];
这就是为什么发生。
你不应该把i
当作柜台。 给你一个indexPath
的委托方法的indexPath
在于告诉你从你的源数据数组中得到什么信息。 所以,删除i
并使用indexPath.row
来代替。
你也不需要2个图像视图。 但你应该保留你的特殊子视图,而不是使用图像视图中的单元格。
你不需要柜台。 如Wain所示,使用indexPath.row
。
重要的是,你不应该在cellForItemAtIndexPath
创build新的子视图,而是使用这种方法来适当地填充内容。 您可以将图像视图放入故事板原型单元格中,并使用标签标识它们。 从dequeueReusableCellWithReuseIdentifier
返回的每个单元格都将包含图像视图。
你不应该在cellForRowAtIndexPath:
创buildui元素。 inheritance一个集合视图单元格,如下所示:
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { NSLog(@"INIT WITH FRAME FOR CELL"); //we create the UIImageView here imageView = [[UIImageView alloc] init]; imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.frame = CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3)); [self.contentView addSubview:imageView]; //the only place we want to do this addSubview: is here! } return self; }
然后添加该子类的单元格作为属性,并更改此代码:]
[collectionView registerClass:[customCellClass class] forCellWithReuseIdentifier:@"MyCell"];
执行这些更改:
-(customCellClass *) collectionView:(UICollectionView *)cV cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = (customCellClass *)[cV dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; cell.backgroundColor = [UIColor blackColor]; imageView.image = [UIImage imageNamed:[storeData objectAtIndex:indexPath.row]]; return cell; }
最后的调整是将[super viewDidLoad]
移动到:
- (void)viewDidLoad { [super viewDidLoad]; //insert the rest of the code here rather than before viewDidLoad }