Невозможно обработать метод setEditing через UIBarButtonItem - PullRequest
0 голосов
/ 25 мая 2011

Я добавил элемент панели кнопок в панель инструментов и реализовал его функциональность в методе, приведенном ниже. Однако я не могу отобразить кнопку удаления в каждой строке. Как я могу это сделать?

-(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 Ответ

0 голосов
/ 25 мая 2011

Кнопка на панели инструментов должна быть связана с действием интерфейса в подклассе контроллера пользовательского табличного представления. Определите действие следующим образом -

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

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


Вам нужно будет реализовать метод делегата tableView:editingStyleForRowAtIndexPath и вернуть UITableViewCellEditingStyleDelete для строк, которые вы хотите включить.
...