Unable to handle setEditing method through UIBarButtonItem
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:n开发者_开发问答il];
[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];
}
}
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.
精彩评论