Kar*_*vam 0 objective-c ios uicollectionview uicollectionviewcell
我正在做一个类似于视频专辑的项目。我UICollectionView用来显示这些视频的拇指图像。最糟糕的是我不应该使用故事板或 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];
}
Run Code Online (Sandbox Code Playgroud)
我已经给了 1 以返回numberOfSectionsInCollectionView和[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;
}
Run Code Online (Sandbox Code Playgroud)
我已经重新检查了myArray. 当视图加载时,集合视图只显示第一张图像。其他 4 个单元格为空。我的代码有什么问题?
对于那些遇到这个问题的人来说,frame是关键。我遇到了这个并改变了:
cell.imageView.frame = cell.frame;
Run Code Online (Sandbox Code Playgroud)
进入
cell.imageView.frame = cell.bounds;
Run Code Online (Sandbox Code Playgroud)
操作正在使用:
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))];
Run Code Online (Sandbox Code Playgroud)
这就是为什么发生这种情况。
| 归档时间: |
|
| 查看次数: |
2910 次 |
| 最近记录: |