Thursday, June 6, 2013

UITableView setEditing animated example in Objective C (iOS).


UITableView setEditing animated

Toggles the receiver into and out of editing mode.

- (void)setEditing:(BOOL)editing animated:(BOOL)animate

Parameters of [UITableView setEditing animated]
editing
YES to enter editing mode, NO to leave it. The default value is NO .
animate
YES to animate the transition to editing mode, NO to make the transition immediate.

Discussion of [UITableView setEditing animated]
When you call this method with the value of editing set to YES, the table view goes into editing mode by calling setEditing:animated: on each visible UITableViewCell object. Calling this method with editing set to NO turns off editing mode. 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. (See UITableViewCell Class Reference for details.) The data source of the table view can selectively exclude cells from editing mode by implementing tableView:canEditRowAtIndexPath:.

UITableView setEditing animated 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 setEditing animated].
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"My View Controller";
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editButtonSelected:)] autorelease];

}

- (void) editButtonSelected: (id) sender
{
    if (tableView1.editing) {
        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editButtonSelected:)] autorelease];
        [self.tableView1 setEditing:NO animated:YES];
    } else {
        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editButtonSelected:)] autorelease];
        [self.tableView1 setEditing:YES animated:YES];

    }

}

UITableView setEditing animated 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 setEditing animated example article.