Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCellDeleteConfirmationControl issue

Tags:

ios

I am using following code in project:

if([NSStringFromClass([subview class])  isEqualToString:@"UITableViewCellDeleteConfirmationControl"])

This is working fine on iOS 5 and 6. But on iOS 7 it always returning NO.

Can anyone tell me is this an issue with iOS 7 or I am doing something wrong?

Thanks!

like image 713
V.J Avatar asked Oct 03 '13 12:10

V.J


2 Answers

Here's my hackish unfinished implementation for people to build from.

This should work for iOS6-- and iOS7.

Obviously this code goes in your subclassed UITableViewCell.

-(void)willTransitionToState:(UITableViewCellStateMask)state{
    NSLog(@"EventTableCell willTransitionToState");
    [super willTransitionToState:state];
    if((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask){
        [self recurseAndReplaceSubViewIfDeleteConfirmationControl:self.subviews];
        [self performSelector:@selector(recurseAndReplaceSubViewIfDeleteConfirmationControl:) withObject:self.subviews afterDelay:0];
    }
}
-(void)recurseAndReplaceSubViewIfDeleteConfirmationControl:(NSArray*)subviews{
    NSString *delete_button_name = @"button_panorama_no_arrow";
    for (UIView *subview in subviews)
    {
        //handles ios6 and earlier
        if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"])
        {
            //we'll add a view to cover the default control as the image used has a transparent BG
            UIView *backgroundCoverDefaultControl = [[UIView alloc] initWithFrame:CGRectMake(0,0, 64, 33)];
            [backgroundCoverDefaultControl setBackgroundColor:[UIColor whiteColor]];//assuming your view has a white BG
               [[subview.subviews objectAtIndex:0] addSubview:backgroundCoverDefaultControl];
            UIImage *deleteImage = [UIImage imageNamed:delete_button_name];
            UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,deleteImage.size.width, deleteImage.size.height)];
            [deleteBtn setImage:[UIImage imageNamed:delete_button_name]];
            [[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
        }
        //the rest handles ios7
        if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationButton"])
        {
            UIButton *deleteButton = (UIButton *)subview;
            [deleteButton setImage:[UIImage imageNamed:delete_button_name] forState:UIControlStateNormal];
            [deleteButton setTitle:@"" forState:UIControlStateNormal];
            [deleteButton setBackgroundColor:[UIColor clearColor]];
            for(UIView* view in subview.subviews){
                if([view isKindOfClass:[UILabel class]]){
                    [view removeFromSuperview];
                }
            }
        }
        if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"])
        {
            for(UIView* innerSubView in subview.subviews){
                if(![innerSubView isKindOfClass:[UIButton class]]){
                    [innerSubView removeFromSuperview];
                }
            }
        }
        if([subview.subviews count]>0){
             [self recurseAndReplaceSubViewIfDeleteConfirmationControl:subview.subviews];
        }

    }
}

I hope this helps someone.

like image 90
braden Avatar answered Nov 16 '22 09:11

braden


Whatever you're doing with that confirmation button relies on internal implementation details of table view cells that Apple is free to change and your solution may stop working after any system update. In your case it seems Apple does not use UITableViewCellDeleteConfirmationControl class in table cells any more.

If you want to support you functionality on iOS 7 you need to check how cell's subview hierarchy was changed there. One of the possible ways to do that may be log -recursiveDescription method on your cell when confirmation button is visible and you will see structure similar to (I stripped some info from logs):

<MyCell:  frame = (0 0; 320 44); >
   | <UITableViewCellScrollView:  frame = (0 0; 320 44);>
   |    | <UITableViewCellDeleteConfirmationView: frame = (320 0; 82 44);>
   |    |    | <UITableViewCellDeleteConfirmationButton: frame = (0 0; 82 43.5);>
   |    |    |    | <UILabel: frame = (15 11; 52 22)>
   |    | <UITableViewCellContentView: frame = (0 0; 287 43.5);>
   |    |    | <UILabel: frame = (15 0; 270 43.5)>
   |    | <_UITableViewCellSeparatorView: frame = (97 43.5; 305 0.5)>
   |    | <UIButton: frame = (297 16; 8 12.5)>
   |    |    | <UIImageView: frame = (0 0; 8 12.5)> 

As you see there's now two private classes that handle confirmation UI - UITableViewCellDeleteConfirmationView and UITableViewCellDeleteConfirmationButton, you probably need to tweak them

like image 3
Vladimir Avatar answered Nov 16 '22 10:11

Vladimir