0

I have added a bar button item in my tool bar and I implemented its functionality in the method given below. I am not able, however, to display a delete button in each row. How can I do this?

-(void)setEditing:(BOOL)editing
         animated:(BOOL)animated
{
    [super setEditing:editing
             animated:animated];
    [editButton setEnabled:!editing];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
    NSString *selectedFile =  (NSString*) [directoryContents objectAtIndex: indexPath.row];
    NSString *selectedPath = [directoryPath stringByAppendingPathComponent:selectedFile];
    BOOL canWrite = [[NSFileManager defaultManager] isWritableFileAtPath:selectedPath];
    if(!canWrite)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"status"
                                message:@"path isnt writable" 
                                delegate:nil 
                                    cancelButtonTitle:@"cancel" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

    NSError *err = nil;
    if(! [[NSFileManager defaultManager] removeItemAtPath:selectedPath error:&err])
    {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"status"
                                                        message:@"cannot delete"
                                                       delegate:nil 
                                              cancelButtonTitle:@"cancel" 
                                              otherButtonTitles:nil];
        [alert1 show];
        [alert1 release];

    }
    // Delete the row from the data source.
    NSArray *deletedPaths = [NSArray arrayWithObject: indexPath];
    [self loadDirectoryContents];
    [self.tableview deleteRowsAtIndexPaths:deletedPaths withRowAnimation:YES];
    }

}

1 Answer 1

0

The button on the toolbar will need to be connected to an interface action in the custom table view controller subclass. Define the action as follows -

- (IBAction)startEditing {
    [self setEditing:YES animated:YES];
}

-(void)setEditing:(BOOL)editing
         animated:(BOOL)animated
{
    [super setEditing:editing
             animated:animated];
    [editButton setEnabled:!editing];
}


You will have to implement the delegate method tableView:editingStyleForRowAtIndexPath and return UITableViewCellEditingStyleDelete for the rows you wish to enable it.

10
  • How did you create the button? Can you put that code in the problem? May 25, 2011 at 9:28
  • i manually add the button in tool bar in IB. and set its connection to IBOutlet in header.
    – user720235
    May 25, 2011 at 9:46
  • How did you wire it up. Did you assign a method to be called when the button is clicked? What does that method do? May 25, 2011 at 9:49
  • i didnt set any method when that button is clicked.. only i do [editButton setEnabled:!editing]; in setEditing method. "editButton" is the UIBarButtonItem. plz reffer to above code
    – user720235
    May 25, 2011 at 9:54
  • Ok so the button is getting disabled when you click on it. May 25, 2011 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.