Skip to content

Instantly share code, notes, and snippets.

@crazytonyli
Created June 20, 2015 11:45
Show Gist options
  • Save crazytonyli/2b340ca9b49143366938 to your computer and use it in GitHub Desktop.
Save crazytonyli/2b340ca9b49143366938 to your computer and use it in GitHub Desktop.
-[UICollectionView reloadSections:] does not remove displaying cells from UICollectionView
@import UIKit;
@interface ViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(self.view.bounds.size.width, 200);
layout.minimumLineSpacing = 0;
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
[self.view addSubview:_collectionView];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"reload");
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
[self logCellCount];
});
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self logCellCount];
}
- (void)logCellCount
{
int count = 0;
for (UIView *view in self.collectionView.subviews) {
if ([view isKindOfClass:[UICollectionViewCell class]]) {
++count;
}
}
NSLog(@"cells: %d", count);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor colorWithRed:(arc4random_uniform(255) / 255.0)
green:(arc4random_uniform(255) / 255.0)
blue:(arc4random_uniform(255) / 255.0)
alpha:1];
return cell;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment