Saturday, June 8, 2013

UITableView tableView editingStyleForRowAtIndexPath example in Objective C (iOS).


UITableView tableView editingStyleForRowAtIndexPath

Asks the delegate for the editing style of a row at a particular location in a table view.

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

Parameters of [UITableView tableView editingStyleForRowAtIndexPath]
tableView
The table-view object requesting this information.
indexPath
An index path locating a row in tableView.

Return Value
The editing style of the cell for the row identified by indexPath.

Discussion of [UITableView tableView editingStyleForRowAtIndexPath]
This method allows the delegate to customize the editing style of the cell located atindexPath. If the delegate does not implement this method and the UITableViewCell object is editable (that is, it has its editing property set to YES), the cell has the UITableViewCellEditingStyleDelete style set for it.

UITableView tableView editingStyleForRowAtIndexPath example.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}

Example of [UITableView tableView editingStyleForRowAtIndexPath].
- (UITableViewCellEditingStyle) tableView:(UITableView *) tableView editingStyleForRowAtIndexPath:(NSIndexPath *) indexPath {
    return ([self.tableView isEditing] && indexPath.row == 0) ? UITableViewCellEditingStyleInsert : UITableViewCellEditingStyleDelete;
}

UITableView tableView editingStyleForRowAtIndexPath example.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// No editing style if not editing or the index path is nil.
if (!indexPath) return UITableViewCellEditingStyleNone;

// No editing style if the tableView has no cells.
if ([self.array count] == 0) return UITableViewCellEditingStyleNone;

// If tableView is not editing, then return delete button.
if (tableView.editing == NO) return UITableViewCellEditingStyleDelete;

// If tableView is editing, then return delete button too.
if (tableView.editing == YES) return UITableViewCellEditingStyleDelete;

// If none of the above are returned, then return \"none\".
return UITableViewCellEditingStyleNone;
}

End of UITableView tableView editingStyleForRowAtIndexPath example article.