2

我想创建4个不同类型的多个collectionViewCells。每个细胞对这四种类型之一有不同的看法。基于用户选择,这些类型的每个视图都可以具有不同的内容。collectionView单元格重叠

我遇到的问题是,当屏幕上显示相同类型的多个视图/单元格时,某些卡片重叠/无法正确加载。

-(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; 
} 

卡是我添加到单元格中的视图。如上所述,这些卡有多种类型。

回答

4

尝试使用:

cell.clipsToBounds = YES; 
5

当您滚动UICollectionView时,消失在屏幕外的单元格将重新用于屏幕上显示的新单元格。这意味着如果在collectionView:cellForItemAtIndexPath:方法中添加子视图,那么当该单元格被重新使用时,它们仍将是单元格视图层次结构的一部分。每次细胞被重新使用时,当您拨打[cell addSubview:card]时,它会添加一个新的子视图。您的卡片子视图将简单堆叠在一起。

看来您正在使用一组Card对象,自定义UIView子类来存储每个用户的一副扑克牌。我会建议您将模型从视图中分离出来 - 将每张卡片存储为一个简单的数据模型,它代表卡片的独立显示方式(请参阅MVC)。然后,您可以创建一个可以显示任何卡的自定义UICollectionViewCell子类。在您的collectionView:cellForItemAtIndexPath:中,您只需根据相应的卡数据重新配置单元的视图即可。这样您就不需要在collectionView:cellForItemAtIndexPath:方法中调用addSubview:

+0

谢谢你,我会看看这个方法用于固定号码我有其他问题;) –