Saturday, June 8, 2013

UITableViewCell setEditing example in Objective C (iOS).


UITableViewCell setEditing

Toggles the receiver into and out of editing mode.

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

Parameters
editing
YES to enter editing mode, NO to leave it. The default value is NO .
animated
YES to animate the appearance or disappearance of the insertion/deletion control and the reordering control, NO to make the transition immediate.

Discussion of [UITableViewCell setEditing]
When you call this method with the value of editing set to YES, and the UITableViewCell object is configured to have controls, the cell shows an insertion (green plus) or deletion control (red minus) on the left side of each cell and a reordering control on the right side. This method is called on each visible cell when the setEditing:animated: method of UITableView is invoked. Calling this method with editing set to NO removes the controls from the cell.

UITableViewCell setEditing example.
- (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];

    }

}

Example of [UITableViewCell setEditing].
Add to your class a private instance variable:

@implementation MyTableViewController {
    BOOL _cellSwiped;
}
Override the setEditing method to look for the _cellSwiped variable and only propagate if we didn't swipe. The part that people seem to be missing is that _cellSwiped needs to be reset back to NO otherwise you will never be able to use the edit button after swiping!

- (void) setEditing:(BOOL)editing animated:(BOOL)animated {
    if (!_cellSwiped) {
        [super setEditing:editing animated:animated];
    } else if (!editing) {
        _cellSwiped = NO;
    }
}
Finally, add this method override to detect the swipe:

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    _cellSwiped = YES;
}

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

    if (animated) {
        [UIView beginAnimations:@"setEditingAnimation" context:nil];
        [UIView setAnimationDuration:0.3];
    }

    if (editing) {
        /* do your offset and resize here */
    } else {
        /* return to the original here*/
    }

    if (animated)
        [UIView commitAnimations];
}

End of UITableViewCell setEditing example article.