Saturday, June 8, 2013

UITableViewCell shouldIndentWhileEditing example in Objective C (iOS).

UITableViewCell shouldIndentWhileEditing

A Boolean value that controls whether the cell background is indented when the table view is in editing mode.

@property(nonatomic) BOOL shouldIndentWhileEditing

Discussion of [UITableViewCell shouldIndentWhileEditing]
The default value is YES. This property is unrelated to indentationLevel. The delegate can override this value in tableView:shouldIndentWhileEditingRowAtIndexPath:. This property has an effect only on table views created in the grouped style (UITableViewStyleGrouped); it has no effect on UITableViewStylePlain table views.

UITableViewCell shouldIndentWhileEditing example.
The shouldIndentWhileEditing property only works with grouped tables. I found that setting an indentation level of -3 does the job for plain tables. Is there a better way? Here's what I'm using now:

    if ( == UITableViewStylePlain) {
        cell.indentationLevel = -3;
    } else if ( == UITableViewStyleGrouped) {
        cell.shouldIndentWhileEditing = FALSE;

Example of [UITableViewCell shouldIndentWhileEditing].
You can set a boolean on the cell to make it not indent. Just add

cell.shouldIndentWhileEditing = NO;
to wherever you create your cell.

UITableViewCell shouldIndentWhileEditing example.
Use the UITableViewDelegate method:

- (BOOL)tableView:(UITableView *)tableView
        shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath

    return NO;
This will work for both grouped and non-grouped UITableView types. However, if you have a grouped tableview, you can use this property on the cell:

cell.shouldIndentWhileEditing = NO;

End of UITableViewCell shouldIndentWhileEditing example article.