简体   繁体   English

UICollectionView仅显示第一项

[英]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.

相关问题 UICollectionView reloadData仅刷新第一项 - UICollectionView reloadData refreshes only first item UICollectionView仅显示数据集中的第一个项目 - UICollectionView displays only first item from dataset UICollectionView .reloadData()仅显示部分 - UICollectionView .reloadData() only shows sections 以编程方式更改 UICollectionView 中的第一项 - Programmatically alter the first item in a UICollectionView 试图在tableView中显示时,for循环仅显示数组中的第一项 - for loop only shows first item in array when trying to display in tableView UISegmentedControl仅显示设备上的第一个项目,但在模拟器中工作 - UISegmentedControl only shows first item on device but works in Simulator UICollectionView仅重新加载第一个单元格 - UICollectionView reload only first cell 使用UICollectionViewFlowLayout在UICollectionView中的第一个项目上进行左边距填充 - Left padding on first item in UICollectionView with UICollectionViewFlowLayout 旋转后将第一个可见项保留在UICollectionView中 - Keep first visible item in UICollectionView after rotation UICollectionView标头在第一个单元格上仅被调用一次 - UICollectionView header is called only once on the first cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM