How To Add Edit Mode To UITableView?
I have a table that currently lets you swipe to delete. I want to have an edit button in nav bar so that the user can change the cell's title text.
I have this so far but nothing happens
UIBarButtonItem * editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(setEditing:animated:)];
[self.navigationItem setLeftBarButtonItem:editButton];
[editButton release];
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
[super setEditing:editing animated:animate];
if(editing)
{
NSLog(@"editMode on");
}
else
{
NSLog(@"Done leave editmode");
}
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the managed object for the given index path
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController o开发者_如何学JAVAbjectAtIndexPath:indexPath]];
NSLog(@"fetched results : \n%@\n",[self.fetchedResultsController fetchedObjects]);
// Commit the change.
NSError *error = nil;
// Update the array and table view.
if (![managedObjectContext save:&error])
{
// Handle the error.
}
}
}
Just have or uncomment this on your viewDidLoad
:
self.navigationItem.rightBarButtonItem = self.editButtonItem;
And implement
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
if(editing)
{
NSLog(@"editMode on");
}
else
{
NSLog(@"Done leave editmode");
}
[super setEditing:editing animated:animate];
}
精彩评论