如何将编辑模式添加到UITableView?

时间:2011-07-20 23:56:38

标签: iphone objective-c cocoa-touch uitableview

我有一张桌子,目前可以滑动删除。我想在导航栏中有一个编辑按钮,以便用户可以更改单元格的标题文本。

到目前为止我有这个但没有任何反应

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 objectAtIndexPath: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.
         }
     }
 }

1 个答案:

答案 0 :(得分:5)

viewDidLoad

上添加或取消注释
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

并实施

- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
if(editing)
{
    NSLog(@"editMode on");
}
else
{
    NSLog(@"Done leave editmode");
}

    [super setEditing:editing animated:animate];

}