Skip to content

Commit a4a1664

Browse files
author
Ernesto Rivera
committed
Avoid crashes when modifying objects of a hidden section
1 parent 0cfd47d commit a4a1664

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

Source/AMBTableViewController.m

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -561,14 +561,14 @@ - (void)moveObjectAtIndex:(NSUInteger)oldIndex
561561
[self updateVisibleObjects];
562562
}
563563

564-
if (self.controller.tableView)
565-
{
566-
NSUInteger sectionIndex = [self.controller.sections indexOfObject:self];
567-
[self.controller.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:oldIndex
568-
inSection:sectionIndex]
569-
toIndexPath:[NSIndexPath indexPathForRow:index
570-
inSection:sectionIndex]];
571-
}
564+
if (!self.controller.tableView || self.hidden)
565+
return;
566+
567+
NSUInteger sectionIndex = [self.controller.sections indexOfObject:self];
568+
[self.controller.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:oldIndex
569+
inSection:sectionIndex]
570+
toIndexPath:[NSIndexPath indexPathForRow:index
571+
inSection:sectionIndex]];
572572
}
573573

574574
- (BOOL)isObjectHidden:(id)object
@@ -680,18 +680,18 @@ - (void)reloadObjects:(NSArray *)objects
680680

681681
- (void)reloadObjectsAtIndexes:(NSIndexSet *)indexSet
682682
{
683-
if (self.controller.tableView)
683+
if (!self.controller.tableView || self.hidden)
684+
return;
685+
686+
NSMutableIndexSet * visibleObjectIndexesToReload = [NSMutableIndexSet indexSet];
687+
[visibleObjectIndexesToReload addIndexes:indexSet];
688+
[visibleObjectIndexesToReload removeIndexes:self.hiddenObjectsIndexSet];
689+
690+
if (visibleObjectIndexesToReload.count)
684691
{
685-
NSMutableIndexSet * visibleObjectIndexesToReload = [NSMutableIndexSet indexSet];
686-
[visibleObjectIndexesToReload addIndexes:indexSet];
687-
[visibleObjectIndexesToReload removeIndexes:self.hiddenObjectsIndexSet];
688-
689-
if (visibleObjectIndexesToReload.count)
690-
{
691-
NSArray * pathsToReload = [self indexPathsForRowIndexes:[self rowIndexSetForVisibleObjectsInIndexSet:visibleObjectIndexesToReload]];
692-
[self.controller.tableView reloadRowsAtIndexPaths:pathsToReload
693-
withRowAnimation:self.controller.reloadAnimation];
694-
}
692+
NSArray * pathsToReload = [self indexPathsForRowIndexes:[self rowIndexSetForVisibleObjectsInIndexSet:visibleObjectIndexesToReload]];
693+
[self.controller.tableView reloadRowsAtIndexPaths:pathsToReload
694+
withRowAnimation:self.controller.reloadAnimation];
695695
}
696696
}
697697

@@ -726,7 +726,7 @@ - (void)scrollToObjectAtIndex:(NSUInteger)index
726726
atScrollPosition:(UITableViewScrollPosition)scrollPosition
727727
animated:(BOOL)animated
728728
{
729-
if (!self.controller.tableView)
729+
if (!self.controller.tableView || self.hidden)
730730
return;
731731

732732
NSIndexSet * indexSet = [self rowIndexSetForVisibleObjectsInIndexSet:[NSIndexSet indexSetWithIndex:index]];
@@ -744,22 +744,22 @@ - (void)scrollToObjectAtIndex:(NSUInteger)index
744744

745745
- (void)insertRowsWithIndexes:(NSIndexSet *)rowIndexSet
746746
{
747-
if (self.controller.tableView)
748-
{
749-
NSArray * indexPaths = [self indexPathsForRowIndexes:rowIndexSet];
750-
[self.controller.tableView insertRowsAtIndexPaths:indexPaths
751-
withRowAnimation:self.controller.insertAnimation];
752-
}
747+
if (!self.controller.tableView || self.hidden)
748+
return;
749+
750+
NSArray * indexPaths = [self indexPathsForRowIndexes:rowIndexSet];
751+
[self.controller.tableView insertRowsAtIndexPaths:indexPaths
752+
withRowAnimation:self.controller.insertAnimation];
753753
}
754754

755755
- (void)deleteRowsWithIndexes:(NSIndexSet *)rowIndexSet
756756
{
757-
if (self.controller.tableView)
758-
{
759-
NSArray * indexPaths = [self indexPathsForRowIndexes:rowIndexSet];
760-
[self.controller.tableView deleteRowsAtIndexPaths:indexPaths
761-
withRowAnimation:self.controller.removeAnimation];
762-
}
757+
if (!self.controller.tableView || self.hidden)
758+
return;
759+
760+
NSArray * indexPaths = [self indexPathsForRowIndexes:rowIndexSet];
761+
[self.controller.tableView deleteRowsAtIndexPaths:indexPaths
762+
withRowAnimation:self.controller.removeAnimation];
763763
}
764764

765765
- (NSIndexSet *)indexSetForObjects:(NSArray *)objects

0 commit comments

Comments
 (0)