【发布时间】:2014-02-25 15:27:32
【问题描述】:
我正在使用 ViewController 以这种方式添加 UICollectionView:
UICollectionViewFlowLayout *collectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
[collectionViewFlowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[collectionViewFlowLayout setMinimumInteritemSpacing:2];
[collectionViewFlowLayout setMinimumLineSpacing:4];
[collectionViewFlowLayout setHeaderReferenceSize:CGSizeMake(320, 30)];
CGRect collectionViewFrame = self.view.bounds;
self.collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:collectionViewFlowLayout];
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:myCellIdentifier];
[self.view addSubview:self.collectionView];
这就是我初始化自定义单元格对象的方式(我确保每个单元格的框架填充了屏幕的整个宽度,以便单元格在 UICollectionView 中一个接一个地垂直渲染):
CGSize mySize = CGSizeMake(320, 158);
self.myTitle = [[UILabel alloc ] initWithFrame:CGRectMake(0, 0, mySize.width, mySize.height)];
[self.myTitle setTextColor:[UIColor whiteColor]];
[self.myTitle setBackgroundColor:[UIColor greenColor]];
[self.myTitle setTextAlignment:NSTextAlignmentLeft];
[self.myTitle setNumberOfLines:1];
[self.myTitle setText:@"My Title"];
[self addSubview:self.myTitle];
这就是我在 initWithNibName 中初始化单元格框架的方式:
CGSize mySize = CGSizeMake(320, 158);
self = [super initWithFrame:CGRectMake(0, 0, mySize.width, mySize.height)];
这是我的 cellForItemAtIndexPath 代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:myCellIdentifier forIndexPath:indexPath];
return cell;
}
现在我有四个这样的单元格,但它们是重叠的。我究竟做错了什么?谢谢!
【问题讨论】:
-
您能展示一下您是如何设置单元格大小的吗?看起来您可能正在将标签设置为比包含它们的单元格更大的框架。
-
请同时发布您的
cellForRowAtIndexPath -
是的,我更新了我的问题!
-
我也遇到了这个问题,如果解决了能告诉我怎么解决吗?
标签: ios iphone uicollectionview