【发布时间】:2013-11-16 12:40:32
【问题描述】:
我正在做一个类似于视频专辑的项目。我使用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];
}
我在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;
}
我已经重新检查了myArray 中的图像。当视图加载时,集合视图仅显示第一张图像。其他 4 个单元格为空。我的代码有什么问题?
【问题讨论】:
标签: ios objective-c uicollectionview uicollectionviewcell