tableView setEditing: animated && canEditRowAtIndexPath && editingStyleForRowAtIndexPath

- (void)startEditing {
    [self.tableView setEditing:YES animated:YES];
    [self.topBar.rightButton setTitle:RIGHT_BUTTON_TITLE_DONE forState:UIControlStateNormal];
    [self.topBar.rightButton addTarget:self action:@selector(doneWithEditing) forControlEvents:UIControlEventTouchUpInside];
}
 
- (void)doneWithEditing {
    [self.tableView setEditing:NO animated:YES];
    [self.topBar.rightButton setTitle:RIGHT_BUTTON_TITLE_EDIT forState:UIControlStateNormal];
    [self.topBar.rightButton addTarget:self action:@selector(startEditing) forControlEvents:UIControlEventTouchUpInside];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!_allowDelete)
        return NO;
    return (indexPath.row != 0);
}
 
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    return [cell isKindOfClass:[IPhoneMVFolderListItemCell class]] ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleNone;
}
 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    IPhoneMVFolderListItemCell* cell = (IPhoneMVFolderListItemCell*)[tableView cellForRowAtIndexPath:indexPath];
    CVFolderListItem* item = (CVFolderListItem*)cell.object;
    
    if ([(NSMutableArray*)[self.rows objectAtIndex:indexPath.section] count] > 1) {
        [(NSMutableArray*)[self.rows objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    } else {
        [self.rows removeObjectAtIndex:indexPath.section];
        [self.sections removeObjectAtIndex:indexPath.section];
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    
    NSMutableArray* folders = [NSMutableArray arrayWithArray:_currentRelatedFolders];
    for (CVFolderListItem* deletedFolder in _currentRelatedFolders) {
        if ([deletedFolder.key isEqualToString:item.key]) {
            [folders removeObject:deletedFolder];
            break;
        }
    }
    _currentRelatedFolders = folders;
    _taskModel.taskItem.folders = folders;
    [self updateTitle];
    
    // call api to delete item from folder at server
    CVAPIRequestModel* model = [[CVAPIRequestModel alloc] init];
    CVAPIRequest*  request = [[CVAPIRequest alloc] initWithAPIPath:[NSString stringWithFormat:FOLDER_REMOVE_ITEMS_API_PATH, item.key]];   
NSDictionary* param = @{@"itemKeys":@[_taskKey]};
    [request setPOSTParamString:[param jsonValue] isJsonFormat:YES];
    
    [self showUpdating:YES];
    [model sendRequest:request completion:^(NSDictionary* apiResult, NSError* error) {
        [self showUpdating:NO];
    }];
     return;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s