Showing posts with label setEditing. Show all posts
Showing posts with label setEditing. Show all posts

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.

Friday, June 7, 2013

UITableView setEditing example in Objective C (iOS).


UITableView setEditing

Toggles the receiver into and out of editing mode.

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

Parameters of [UITableView setEditing]
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]
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 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].
 [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 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 example article.

Thursday, June 6, 2013

UITableView setEditing example in Objective C (iOS).


UITableView setEditing

Toggles the receiver into and out of editing mode.

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

Parameters of [UITableView setEditing]
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]
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 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].
- (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 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 example article.