Skip to content

Commit a31c9de

Browse files
author
Ernesto Rivera
committed
New methods to move section objects
1 parent 726dede commit a31c9de

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

Source/AMBTableViewController.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,20 @@ typedef void (^AMBTableViewCellConfigurationBlock)(id object,
274274
/// @param indexSet The indexes of the objects to be removed.
275275
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexSet;
276276

277+
/// @name Moving Objects
278+
279+
/// Move an object to a given index.
280+
/// @param object The object to move.
281+
/// @param index The index to move the object to.
282+
- (void)moveObject:(id)object
283+
toIndex:(NSUInteger)index;
284+
285+
/// Move an object from a given index to a new one.
286+
/// @param oldIndex The original object index.
287+
/// @param index The index to move the object to.
288+
- (void)moveObjectAtIndex:(NSUInteger)oldIndex
289+
toIndex:(NSUInteger)index;
290+
277291
/// @name Hiding and Showing Objects
278292

279293
/// Whether an object is hidden.

Source/AMBTableViewController.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,42 @@ - (void)removeObjectsAtIndexes:(NSIndexSet *)indexSet
511511
[self deleteRowsWithIndexes:[self rowIndexSetForVisibleObjectsInIndexSet:indexSet]];
512512
}
513513

514+
- (void)moveObject:(id)object
515+
toIndex:(NSUInteger)index
516+
{
517+
[self moveObjectAtIndex:[self.objects indexOfObject:object]
518+
toIndex:index];
519+
520+
}
521+
522+
- (void)moveObjectAtIndex:(NSUInteger)oldIndex
523+
toIndex:(NSUInteger)index
524+
{
525+
id object = _mutableObjects[oldIndex];
526+
[_mutableObjects removeObjectAtIndex:oldIndex];
527+
[_mutableObjects insertObject:object
528+
atIndex:index];
529+
530+
if ([_hiddenObjectsMutableIndexSet containsIndex:oldIndex])
531+
{
532+
[_hiddenObjectsMutableIndexSet removeIndex:oldIndex];
533+
[_hiddenObjectsMutableIndexSet addIndex:index];
534+
}
535+
else
536+
{
537+
[self updateVisibleObjects];
538+
}
539+
540+
if (self.controller.tableView)
541+
{
542+
NSUInteger sectionIndex = [self.controller.sections indexOfObject:self];
543+
[self.controller.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:oldIndex
544+
inSection:sectionIndex]
545+
toIndexPath:[NSIndexPath indexPathForRow:index
546+
inSection:sectionIndex]];
547+
}
548+
}
549+
514550
- (BOOL)isObjectHidden:(id)object
515551
{
516552
return [self isObjectAtIndexHidden:[self.objects indexOfObject:object]];

0 commit comments

Comments
 (0)