Skip to content

Instantly share code, notes, and snippets.

@paulw11
Created February 10, 2017 00:23
Show Gist options
  • Save paulw11/025e6f3dbdfaf911a6dab95c6631cbe2 to your computer and use it in GitHub Desktop.
Save paulw11/025e6f3dbdfaf911a6dab95c6631cbe2 to your computer and use it in GitHub Desktop.
//
// ViewController.m
// cvtest
//
// Created by Paul Wilkinson on 10/2/17.
// Copyright © 2017 Paul Wilkinson. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController {
UICollectionView *_collection;
NSArray *_values;
}
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
_collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collection.dataSource = self;
_values = @[[UIColor redColor], [UIColor greenColor]];
[_collection registerClass:[UICollectionViewCell class]
forCellWithReuseIdentifier:@"reuse"];
[self.view addSubview:_collection];
_collection.frame = self.view.bounds;
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self triggerBatchUpdate];
});
}
- (void)triggerBatchUpdate {
[_collection performBatchUpdates:^{
_values = @[[UIColor blueColor], [UIColor yellowColor], [UIColor redColor]];
[_collection moveItemAtIndexPath:[self index:0] toIndexPath:[self index:2]];
[_collection insertItemsAtIndexPaths:@[[self index:0]]];
// Works with this line
// [_collection moveItemAtIndexPath:[self index:1] toIndexPath:[self index:1]];
// Fails with this line
//[_collection reloadItemsAtIndexPaths:@[[self index:1]]];
} completion:^(BOOL finished) {
[_collection reloadItemsAtIndexPaths:@[[self index:1]]];
}];
}
- (NSIndexPath *)index:(NSUInteger)ind {
return [NSIndexPath indexPathForRow:ind inSection:0];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section {
return _values.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [_collection dequeueReusableCellWithReuseIdentifier:@"reuse"
forIndexPath:indexPath];
cell.backgroundColor = _values[indexPath.item];
return cell;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment