Help get this topic noticed by sharing it on Twitter, Facebook, or email.

Adding Refactoring to Moving Table Cells Example

In IOS 5 Programming Cookbook, the section on Moving Cells and Sections in Table Views (3.11) presents a good opportunity for a refactor that might be instructional. I lifted the code that both updates the section arrays and moves the cells into a separate function called moveDataFromSection:andRow:toDestinationSection:andDestinationRow:

This allow the moveCell1InSection1ToCell2InSection1 to be a no brainer and extends to the next area that introduces moveCell2InSection1ToCell1InSection2. Here's a primitive implementation:

- (void)moveDataFromSection:(NSUInteger)section andRow:(NSUInteger)row toDestinationSection:(NSUInteger)destinationSection andDestinationRow:(NSUInteger)destinationRow
{
NSMutableArray *sourceSectionArray = [self.arrayOfSections objectAtIndex:section];
NSMutableArray *destinationSectionArray = [self.arrayOfSections objectAtIndex:destinationSection];

NSString *sourceCellInSection = [sourceSectionArray objectAtIndex:row];
[sourceSectionArray removeObject:sourceCellInSection];
[destinationSectionArray insertObject:sourceCellInSection atIndex:destinationRow];

NSIndexPath *sourceIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
NSIndexPath *destinationIndexPath = [NSIndexPath indexPathForRow:destinationRow inSection:destinationSection];
[self.myTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
1 person likes
this idea
+1
Reply