UICollectionView 单元格未在 reloadData 上清除
Posted
技术标签:
【中文标题】UICollectionView 单元格未在 reloadData 上清除【英文标题】:UICollectionView Cells not clearing on reloadData 【发布时间】:2014-06-05 10:35:01 【问题描述】:我遇到了与这些类似的问题,但似乎无法解决我的问题..
-
UICollectionView reload data Issue
ios UICollectionView cells not refreshing after scroll
UILabel overloading in UICollectionView
我在cell
中有一个带有自定义UIViews
的CollectionView
,在reloadData 上,它重新添加了自定义UIViews
,而不是删除已经显示的那些。这是我的 cellForItemAtIndexPath...
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
UICollectionViewCell *collectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LiveTileCell" forIndexPath:indexPath];
NSDictionary *cellDataDictionary = [self.dataArray objectAtIndex:indexPath.row];
UIView *customView = [[UIView alloc] initWithFrame:collectionViewCell.contentView.frame];
customView.tag = 1002;
LiveTileView *liveTile = [[LiveTileView alloc] initWithFrame:CGRectMake(0, 0, collectionViewCell.frame.size.width, collectionViewCell.frame.size.height)];
liveTile.tag = 1001;
[liveTile initialize:[cellDataDictionary valueForKey:@"backgroundColor"] titleFont:[cellDataDictionary valueForKey:@"titleFont"] titleText:[cellDataDictionary valueForKey:@"titleText"] titleColor:[UIColor whiteColor] titleImage:[cellDataDictionary valueForKey:@"titleImage"] badge:[[cellDataDictionary valueForKey:@"badge"] intValue] detailViewImage:[cellDataDictionary valueForKey:@"detailViewImage"] compactView:nil isLiveTile:[[cellDataDictionary valueForKey:@"isLiveTile"] boolValue] animationTime:[[cellDataDictionary valueForKey:@"animationTime"] floatValue] elementIndex:indexPath.item];
[customView addSubview:liveTile];
[collectionViewCell addSubview:customView];
return collectionViewCell;
任何帮助都会很棒..提前感谢
【问题讨论】:
【参考方案1】:试试这个
UICollectionViewCell *collectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LiveTileCell" forIndexPath:indexPath];
// for(UIView *view in collectionViewCell.contentView.subviews)
for(UIView *view in collectionViewCell.subviews)
for(UIView *subView in view.subviews)
[subView removeFromSuperview];
[view removeFromSuperview];
// write your code here
【讨论】:
为什么会这样?它正在删除您现有的视图,然后添加您的新自定义视图。 让它工作了.. 将collectionViewCell.contentView.subviews
更改为collectionViewCell.subviews
,现在它工作得很好:) 谢谢以上是关于UICollectionView 单元格未在 reloadData 上清除的主要内容,如果未能解决你的问题,请参考以下文章
UICollectionView 正在显示,但单元格未快速显示
UICollectionView estimatedItemSize - 最后一个单元格未对齐