Thursday, June 6, 2013

UITableView editing example in Objective C (iOS).


UITableView editing

A Boolean value that determines whether the receiver is in editing mode.

@property(nonatomic, getter=isEditing) BOOL editing

Discussion of [UITableView editing]
When the value of this property is YES , the table view is in editing mode: the cells of the table might show an insertion or deletion control on the left side of each cell and a reordering control on the right side, depending on how the cell is configured. (SeeUITableViewCell Class Reference for details.) Tapping a control causes the table view to invoke the data source method tableView:commitEditingStyle:forRowAtIndexPath:. The default value is NO.

UITableView editing example.
What you should is override UIViewController's setEditing method in your own controller class:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    if(editing == YES)
    {
        // Your code for entering edit mode goes here
    } else {
        // Your code for exiting edit mode goes here
    }
}

Example of [UITableView editing].

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection HAS BEEN CALLED");
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    int rows = [sectionInfo numberOfObjects];

    if (self.editing) {
        if (!editingFromSwipe && tableIsEditing) {
            NSLog(@"Returning extra row - editing not from swipe \n");
            return rows +1;
        }
        NSLog(@"Returning normal rows - editing from swipe \n");
        return rows;
    }
    NSLog(@"Returning normal rows - not editing - and setting tableIsEditing back to NO \n");
    tableIsEditing = NO;
    return rows;
}

UITableView editing example.
- (IBAction) EditTable:(id)sender

{

    if(self.editing)

    {

      [super setEditing:NO animated:NO];

      [Table setEditing:NO animated:NO];

      [Table reloadData];

        [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];

        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];

    }

    else

    {

      [super setEditing:YES animated:YES];

      [Table setEditing:YES animated:YES];

      [Table reloadData];

        [self.navigationItem.leftBarButtonItem setTitle:@"Done"];

        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];

    }

}

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

      if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;

      if (self.editing && indexPath.row == ([arry count]))

    {

      return UITableViewCellEditingStyleInsert;

    } else

    {

      return UITableViewCellEditingStyleDelete;

    }

    return UITableViewCellEditingStyleNone;

}

- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

{

   if (editingStyle == UITableViewCellEditingStyleDelete)

    {

        [arry removeObjectAtIndex:indexPath.row];

      [Table reloadData];

    } else if (editingStyle == UITableViewCellEditingStyleInsert)

    {

        [arry insertObject:@"Tutorial" atIndex:[arry count]];

      [Table reloadData];

    }

}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath

      toIndexPath:(NSIndexPath *)toIndexPath

{

    NSString *item = [[arry objectAtIndex:fromIndexPath.row] retain];

    [arry removeObject:item];

    [arry insertObject:item atIndex:toIndexPath.row];

    [item release];

}
}

End of UITableView editing example article.