Friday, June 7, 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]
YES to enter editing mode, NO to leave it. The default value is NO .
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.
- (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 [UITableView setEditing animated].
 [self.tblView setEditing:YES animated:YES];
 [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(onLoadTable) userInfo:nil repeats:NO];
-(void) onLoadTable
    [self.tblView reloadData];

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

End of UITableView setEditing animated example article.