Saturday, June 8, 2013

UITableViewDataSource tableView commitEditingStyle forRowAtIndexPath example in Objective C (iOS).


UITableViewDataSource tableView commitEditingStyle forRowAtIndexPath

Asks the data source to commit the insertion or deletion of a specified row in the receiver.

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

Parameters of [UITableViewDataSource tableView commitEditingStyle forRowAtIndexPath]
tableView
The table-view object requesting the insertion or deletion.
editingStyle
The cell editing style corresponding to a insertion or deletion requested for the row specified by indexPath. Possible editing styles are UITableViewCellEditingStyleInsert or UITableViewCellEditingStyleDelete.
indexPath
An index path locating the row in tableView.

Discussion of [UITableViewDataSource tableView commitEditingStyle forRowAtIndexPath]
When users tap the insertion (green plus) control or Delete button associated with a UITableViewCell object in the table view, the table view sends this message to the data source, asking it to commit the change. (If the user taps the deletion (red minus) control, the table view then displays the Delete button to get confirmation.) The data source commits the insertion or deletion by invoking the UITableView methods insertRowsAtIndexPaths:withRowAnimation: or deleteRowsAtIndexPaths:withRowAnimation:, as appropriate.[UITableViewDataSource tableView commitEditingStyle forRowAtIndexPath]

To enable the swipe-to-delete feature of table views (wherein a user swipes horizontally across a row to display a Delete button), you must implement this method.

You should not call setEditing:animated: within an implementation of this method. If for some reason you must, invoke it after a delay by using the performSelector:withObject:afterDelay: method.

UITableViewDataSource tableView commitEditingStyle forRowAtIndexPath example.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [DummyData removeObjectAtIndex:indexPath.row];
        [_tableView reloadData];
    }      
}

Example of [UITableViewDataSource tableView commitEditingStyle forRowAtIndexPath].
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [self.reports removeObjectAtIndex:indexPath.row];
        [tbl reloadData];
}  

UITableViewDataSource tableView commitEditingStyle forRowAtIndexPath example.

- ( void )tableView:( UITableView * )tableView
 commitEditingStyle:( UITableViewCellEditingStyle )editingStyle
  forRowAtIndexPath:( NSIndexPath * )indexPath
{
    if ( self.isEditable ) {
        // Allow swipe.
    } else {
        // Disallow swipe.
    }
}

End of UITableViewDataSource tableView commitEditingStyle forRowAtIndexPath example article.