[英]collectionView cells overlapping
我正在尝试使用4种不同的类型创建多个collectionViewCells。 并且每个单元格对这4种类型中的一种具有不同的视图。 根据用户选择,这些类型的每个视图都可以具有不同的内容。
我遇到的问题是,当屏幕上有多个相同类型的视图/单元时,某些卡重叠/未正确加载。
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
Card *card = [[[usermanager getSelectedUser] getCards] objectAtIndex:indexPath.item];
NSLog(@"CARD LOADING: %@", card.title);
[card setupLayout];
UICollectionViewCell *cell;
if(card.type.intValue == 1){
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"lifestyleCell" forIndexPath:indexPath];
}else if(card.type.intValue == 2){
cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"sceneCell" forIndexPath:indexPath];
}else if(card.type.intValue == 3){
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"energyCell" forIndexPath:indexPath];
}else if(card.type.intValue == 4){
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"productCell" forIndexPath:indexPath];
}else{
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cardCell" forIndexPath:indexPath];
}
[cell addSubview:card];
//Add dropshadow
cell.contentView.layer.borderWidth = 1.0f;
cell.contentView.layer.borderColor = [UIColor clearColor].CGColor;
cell.contentView.layer.masksToBounds = YES;
cell.layer.shadowColor = [UIColor blackColor].CGColor;
cell.layer.shadowOffset = CGSizeMake(0, 5.0f);
cell.layer.shadowRadius = 2.0f;
cell.layer.shadowOpacity = 0.5f;
cell.layer.masksToBounds = NO;
return cell;
}
卡片是我添加到单元格的视图。 如上所述,这些卡有多种类型。
滚动UICollectionView
,屏幕外消失的单元格将重新用于屏幕上显示的新单元格。 这意味着如果在collectionView:cellForItemAtIndexPath:
方法中添加子视图,则在重新使用该单元格时,它们仍将是单元格视图层次结构的一部分。 每次重新使用单元格时,当您调用[cell addSubview:card]
时,它将添加一个新的子视图。 您的卡子视图将简单地叠加在一起。
您似乎正在使用Card
对象的集合,自定义UIView
子类来存储每个用户的卡片组。 我建议你将模型从视图中分离出来 - 将每张卡存储为一个简单的数据模型,它代表卡片的独立显示方式(参见MVC)。 然后,您可以创建一个可以显示任何卡的自定义UICollectionViewCell
子类。 在您的collectionView:cellForItemAtIndexPath:
您只需根据相应的卡片数据重新配置单元格的视图。 这样你就不需要在你的collectionView:cellForItemAtIndexPath:
方法中调用addSubview:
尝试使用:
cell.clipsToBounds = YES;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.