[英]UICollectionView shows only the first item
I'm working on a project similar to a video album. 我正在做一个类似于视频专辑的项目。 In that I'm using
UICollectionView
to display the thumb images of those videos. 在这种情况下,我使用
UICollectionView
来显示这些视频的缩略图。 The worst part is that I should not use storyboard or xib files. 最糟糕的是我不应该使用情节提要或Xib文件。 I have tried to do this programatically.
我试图以编程方式执行此操作。 I'm currently working on this code:
我目前正在处理此代码:
- (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];
}
I have given 1 for return in numberOfSectionsInCollectionView
and [myArray count]
for return in numberOfItemsInSection
. 我给
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;
}
I have rechecked the images in myArray
. 我已经检查了
myArray
的图像。 When the view loads, the collection view shows only the first image. 加载视图时,集合视图仅显示第一张图像。 Other 4 cells are empty.
其他4个单元为空。 What is wrong with my code?
我的代码有什么问题?
For those who is experiencing this problem, frame
is the key. 对于那些遇到此问题的人来说,
frame
是关键。 I've encountered this and changed: 我遇到了这个并且改变了:
cell.imageView.frame = cell.frame;
into 成
cell.imageView.frame = cell.bounds;
The op is using: 该操作正在使用:
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))];
That's why this happened. 这就是为什么发生这种情况。
You shouldn't be using i
as a counter. 您不应该将
i
用作计数器。 The whole point of the delegate method sending you an indexPath
is that it tells you what information to get from your array of source data. 给您发送
indexPath
的委托方法的全部要点是,它告诉您要从源数据数组中获取什么信息。 So, remove i
and use the indexPath.row
instead. 因此,删除
i
并改用indexPath.row
。
You also don't need 2 image views. 您也不需要2个图像视图。 But you should probably keep your special subview and not use the cells built in image view.
但是您可能应该保留自己的特殊子视图,而不要使用图像视图中内置的单元格。
You do not need a counter. 您不需要柜台。 As indicated by Wain, use
indexPath.row
. 如Wain所示,使用
indexPath.row
。
Importantly, you should not create new subviews in cellForItemAtIndexPath
, but rather use this method to fill them appropriately with content. 重要的是,您不应在
cellForItemAtIndexPath
创建新的子视图,而应使用此方法用内容适当地填充它们。 You could put the image views into your storyboard prototype cells and identify them with tags. 您可以将图像视图放入情节提要原型单元中,并使用标签进行标识。 Each cell returned from
dequeueReusableCellWithReuseIdentifier
will already contain the image view. 从
dequeueReusableCellWithReuseIdentifier
返回的每个单元格将已经包含图像视图。
you should never create ui elements in cellForRowAtIndexPath:
. 您永远不要在
cellForRowAtIndexPath:
创建ui元素。 Subclass a collection view cell like so: 子类化集合视图单元格,如下所示:
- (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;
}
Then add that subclassed cell as a property and alter this code:] 然后将该子类化的单元格添加为属性并更改此代码:]
[collectionView registerClass:[customCellClass class] forCellWithReuseIdentifier:@"MyCell"];
The perform these changes: 执行以下更改:
-(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;
}
A final adjustment would be to move the the [super viewDidLoad]
to this: 最后的调整是将
[super viewDidLoad]
移至此位置:
- (void)viewDidLoad
{
[super viewDidLoad];
//insert the rest of the code here rather than before viewDidLoad
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.