1. /**UICollectionView的初始化*/
UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
UICollectionView * _colview=[[UICollectionView alloc] initWithFrame:CGRectMake(0,0,width,height) collectionViewLayout:layout];
_colview.backgroundColor=[UIColor whiteColor];
[self.mainsclview addSubview:_colview];
[_colview registerClass:[FirstCollectionCell class]
forCellWithReuseIdentifier:@"Cell"];
_colview.delegate=self;
_colview.dataSource=self;
2.实现delegate的方法-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath中添加重用代码
static NSString * CellIdentifier = @"Cell";
FirstCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
for (UIView *v in cell.contentView.subviews)
{
[v removeFromSuperview];
}
或者
for (UIView *v in cell.contentView.subviews)
{
[v removeFromSuperview];
}
取决于你讲子视图添加进去cell,还是cell.contentview中。这样才不回出现往复出现。4.在
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 中设置cell的高度。