1
我有一个UICollectionView,我想用100个UICollectionViewCells填充它,每个UICollectionViewCells都有自己的唯一UILabel和从数组中获取的文本。我想以编程方式执行此操作(没有故事板)。UICollectionViewCells与UICollectionView中的数据以编程方式从数组中填充数据
我试过它,因为下面发布,但由于某种原因只有第一个单元格正确渲染。
// Setting up the UICollectionView
- (void)setupCollectionView {
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
CGRect newFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * 0.5);
self.collectionView=[[UICollectionView alloc] initWithFrame:newFrame collectionViewLayout:layout];
[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[self.collectionView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.collectionView];
}
//Trying to generate the unique cells with data
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor yellowColor];
UILabel *label = [[UILabel alloc] initWithFrame: cell.frame];
label.text = [self.array objectAtIndex: indexPath.row];
label.textColor = [UIColor blackColor];
[cell.contentView addSubview:label];
return cell;
}
注意:我的数组大小为100,随机生成数字。
您的帮助表示赞赏:)
谢谢!
感谢您的帮助丹。我一直在用这个问题猛撞我的脑袋,直到你出现为止! 这个问题的确切诊断和解决方案的很好的解释。 10/10 :) – justinSYDE