Bla*_*gic 2 user-interface objective-c ios uicollectionview uicollectionviewcell
我正在尝试使用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;
}
Run Code Online (Sandbox Code Playgroud)
卡片是我添加到单元格的视图.如上所述,这些卡有多种类型.
当您滚动a时UICollectionView,屏幕外消失的单元格将重新用于屏幕上显示的新单元格.这意味着如果在collectionView:cellForItemAtIndexPath:方法中添加子视图,则在重新使用该单元格时,它们仍将是单元格视图层次结构的一部分.每次重新使用单元格时,它都会在您调用时添加新的子视图[cell addSubview:card].您的卡子视图将简单地叠加在一起.
您似乎正在使用一组Card对象(自定义UIView子类)来存储每个用户的卡片组.我建议你将模型从视图中分离出来 - 将每张卡存储为一个简单的数据模型,它代表卡的独立显示方式(参见MVC).然后,您可以创建一个UICollectionViewCell可以显示任何卡的自定义子类.您collectionView:cellForItemAtIndexPath:可以根据相应的卡数据重新配置单元格的视图.这样你就不需要addSubview:在你的collectionView:cellForItemAtIndexPath:方法中调用了.
| 归档时间: |
|
| 查看次数: |
2572 次 |
| 最近记录: |