Saturday, June 8, 2013

UITableView tableView shouldIndentWhileEditingRowAtIndexPath example in Objective C (iOS).


UITableView tableView shouldIndentWhileEditingRowAtIndexPath

Asks the delegate whether the background of the specified row should be indented while the table view is in editing mode.

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

Parameters of [UITableView tableView shouldIndentWhileEditingRowAtIndexPath]
tableView
The table-view object requesting this information.
indexPath
An index-path object locating the row in its section.

Return Value
YES if the background of the row should be indented, otherwise NO.

Discussion of [UITableView tableView shouldIndentWhileEditingRowAtIndexPath]
If the delegate does not implement this method, the default is YES. This method is unrelated to tableView:indentationLevelForRowAtIndexPath:.

UITableView tableView shouldIndentWhileEditingRowAtIndexPath example.
// UITableViewDelegate

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

Example of [UITableView tableView shouldIndentWhileEditingRowAtIndexPath].
- (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {

  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:.3];

  [cell.CellTitle setFrame:cgrectMake(x+20, y, w, h)];  // +x
  [...]
  [cell.CellAddress setFrame:f2];

  [UIView commitAnimations];

  return YES;
}

UITableView tableView shouldIndentWhileEditingRowAtIndexPath example.
// Override to prevent indentation of cells in editing mode (in theory)
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}
I also used the code below to stop the insert/delete functionality and enable the rows can be moved (easily switched off).

// Select the editing style of each cell
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        // Do not allow inserts / deletes
        return UITableViewCellEditingStyleNone;
    }

// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
     // Return NO if you do not want the item to be re-orderable.
     return YES;
}

End of UITableView tableView shouldIndentWhileEditingRowAtIndexPath example article.