Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
UITableView cell deselection
- (void)eln_clearSelectionOnViewWillAppearForView:(__kindof UIScrollView *)view {
NSParameterAssert([view isKindOfClass:[UITableView class]] || [view isKindOfClass:[UICollectionView class]]);
if ([view isKindOfClass:[UICollectionView class]]) {
// collection view
UICollectionView *collectionView = (UICollectionView *)view;
NSArray *indexPaths = collectionView.indexPathsForSelectedItems;
// deselect
[indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *obj, NSUInteger idx, BOOL *stop) {
[collectionView deselectItemAtIndexPath:obj animated:YES];
}];
[self eln_performTransitionCompletionHandlerWithBlock:^(BOOL cancelled) {
if (cancelled)
// select back
[indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *obj, NSUInteger idx, BOOL *stop) {
[collectionView selectItemAtIndexPath:obj
animated:YES
scrollPosition:UICollectionViewScrollPositionNone];
}];
}];
} else if ([view isKindOfClass:[UITableView class]]) {
UITableView *tableView = (UITableView *)view;
NSArray *indexPaths = tableView.indexPathsForSelectedRows;
// deselect
[indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *obj, NSUInteger idx, BOOL *stop) {
[tableView deselectRowAtIndexPath:obj animated:YES];
}];
[self eln_performTransitionCompletionHandlerWithBlock:^(BOOL cancelled) {
if (cancelled)
// select back
[indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *obj, NSUInteger idx, BOOL *stop) {
[tableView selectRowAtIndexPath:obj animated:YES scrollPosition:UITableViewScrollPositionNone];
}];
}];
}
}
- (void)eln_performTransitionCompletionHandlerWithBlock:(void (^)(BOOL cancelled))completion {
if (!completion)
return;
[self.transitionCoordinator notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) {
completion([context isCancelled]);
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment