Saturday, June 8, 2013

UITableViewCell UITableViewCellEditingStyleDelete example in Objective C (iOS).


UITableViewCell UITableViewCellEditingStyleDelete

Cell Editing Style
The editing control used by a cell.

typedef enum {
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert
} UITableViewCellEditingStyle;

Constants
UITableViewCellEditingStyleNone
The cell has no editing control. This is the default value.
UITableViewCellEditingStyleDelete
The cell has the delete editing control; this control is a red circle enclosing a minus sign.
UITableViewCellEditingStyleInsert
The cell has the insert editing control; this control is a green circle enclosing a plus sign.

Discussion of [UITableViewCell UITableViewCellEditingStyleDelete]
You use them to set the value of the editingStyle property.

UITableViewCell UITableViewCellEditingStyleDelete example.
// Override to support conditional editing of the table view.
// This only needs to be implemented if you are going to be returning NO
// for some items. By default, all items are editable.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
    }   
}

Example of [UITableViewCell UITableViewCellEditingStyleDelete].
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source

    [categoryArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    [deleteCategoryTable reloadData];

    NSString *selectedCategory = [categoryArray objectAtIndex:indexPath.row];

    sqlite3 *db= [KeyCryptAppAppDelegate getNewDBConnection];
    KeyCryptAppAppDelegate *appDelegate = (KeyCryptAppAppDelegate *)[[UIApplication sharedApplication] delegate];
    const char *sql = [[NSString stringWithFormat:@"Delete from Categories where Category = '%@';", selectedCategory]cString];

    sqlite3_stmt *compiledStatement;

    if (sqlite3_prepare_v2(db, sql, -1, &compiledStatement, NULL)==SQLITE_OK)
    {
        sqlite3_exec(db,sql,NULL,NULL,NULL);

    }
    else {
        NSAssert1(0,@"Error preparing statement", sqlite3_errmsg(db));
    }
    sqlite3_finalize(compiledStatement);
}

UITableViewCell UITableViewCellEditingStyleDelete example.
//Updating the data-model array and deleting the row
- (void)tableView:(UITableView *)tv commitEditingStyle:    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
            [self.tagsArray removeObjectAtIndex:indexPath.row];
        [self.addTagTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

End of UITableViewCell UITableViewCellEditingStyleDelete example article.