Saturday, June 8, 2013

UITableViewCell showingDeleteConfirmation example in Objective C (iOS).


UITableViewCell showingDeleteConfirmation

Returns whether the cell is currently showing the delete-confirmation button. (read-only)

@property(nonatomic, readonly) BOOL showingDeleteConfirmation

Discussion of [UITableViewCell showingDeleteConfirmation]
When users tap the deletion control (the red circle to the left of the cell), the cell displays a "Delete" button on the right side of the cell; this string is localized.

UITableViewCell showingDeleteConfirmation example.
[cell addObserver: self forKeyPath: @"showingDeleteConfirmation"
          options: NSKeyValueObservingOptionNew context: NULL];
Then you implement the observer callback method:

- (void)observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object
                        change: (NSDictionary *) change context: (void *) context
{
    if ( [keyPath isEqualToString: @"showingDeleteConfirmation"] )
    {
        UITableViewCell * cell = (UITableViewCell *) object;
        BOOL isShowing = [[change objectForKey: NSKeyValueChangeNewKey] boolValue];
        if ( isShowing == NO )
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        else
            cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

Example of [UITableViewCell showingDeleteConfirmation].
//After validation fails....
UITableViewCell *aCell; 
aCell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
// validations are done and you need to ignore the delete
if ( aCell.showingDeleteConfirmation ){
    aCell.editingAccessoryView = nil;
    aCell.editingAccessoryType = UITableViewCellAccessoryNone;

}

UITableViewCell showingDeleteConfirmation example.
@implementation CustomCell
- (void)layoutSubviews {
       [super layoutSubviews];
       if (self.showingDeleteConfirmation) {
             if ([self.subviews count] < 4) return;
             UIView *delBtn = [self.subviews objectAtIndex:3];
             delBtn.frame = CGRectOffset(delBtn.frame, 0, 10);
       }
}
@end

End of UITableViewCell showingDeleteConfirmation example article.