[Suggestion] Repetitive code


#1

Hi Joe,

Just a quick suggestion on code factoring.

From:

- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
  // Always call super implementation of this method, it needs to do work
  [super setEditing:flag animated];

  // You need to insert/remove a new row in to table view
  if (flag) {
      // If entering edit mode, we add another row to our table view
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[possessions count] inSection:0];
      NSArray *paths = [NSArray arrayWithObject:indexPath];
      [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];
  } else {
      // If leaving edit mode, we remove last row from table view
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[possessions count] inSection:0];
      NSArray *paths = [NSArray arrayWithObject:indexPath];
      [[self tableView] deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
  }
}

To:

- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
  // Always call super implementation of this method, it needs to do work
  [super setEditing:flag animated];

  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[possessions count] inSection:0];
  NSArray *paths = [NSArray arrayWithObject:indexPath];

  // You need to insert/remove a new row in to table view
  if (flag) {
      // If entering edit mode, we add another row to our table view
      [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];
  } else {
      // If leaving edit mode, we remove last row from table view
      [[self tableView] deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
  }
}