开发者

iPhone:Add & Delete Row in UITableview with editing mode

开发者 https://www.devze.com 2023-04-09 09:28 出处:网络
What I want to do i开发者_Python百科s: Setting editing mode on NavigationBar in UITableView adds an edit button on the left side of the UINavigationBar.When I click this button, I\'d like an add but

What I want to do i开发者_Python百科s:

  1. Setting editing mode on NavigationBar in UITableView adds an edit button on the left side of the UINavigationBar. When I click this button, I'd like an add button to appear on the right side of the NavigationBar.

  2. When I click on the add button, add a row to the NSMutableArray and update the table.

So please give me ideas, code, or links to develop this functionality.


- (IBAction)DeleteButtonAction:(id)sender
{
    [arry removeLastObject];

    [Table reloadData];
}


- (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];

}
}


Create a private property to have a reference to the Edit button:

@property (nonatomic) UIBarButtonItem *editButton;

In viewDidLoad:, init the button and assign it to the right button of the navigationItem:

self.editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit", nil) style:UIBarButtonItemStylePlain target:self action:@selector(toggleEdit:)];
self.navigationItem.rightBarButtonItem = self.editButton;

Create a method to invoke when the Edit button is tapped. Will change the table's editing mode and update the Edit button title:

- (IBAction)toggleEdit:(id)sender {

    [self.myTableView setEditing:!self.myTableView.editing animated: YES];
    [self.editButton setTitle:self.myTableView.editing ? NSLocalizedString(@"Done", nil) : NSLocalizedString(@"Edit", nil)];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消