5

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!

V.J
  • 189
  • 3
  • 17
  • Hello barden Thanks!! for your suggestion. I have posted my code that I used to fix this bug. – V.J Nov 08 '13 at 05:20

3 Answers3

27

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.

braden
  • 1,496
  • 17
  • 31
  • 2
    In iOS7 it works because by using `performSelector:withObject:afterDelay` the selector is queued on the thread’s run loop and not performed immediately, allowing the OS to add the Delete button view in the meantime. – HyBRiD Jan 09 '14 at 12:10
  • +1 upvote. But in iOS7 It's not showing text what I am applying to deleteButton. – Nirav Jain Jan 12 '14 at 06:31
  • @Nirav try using [deleteButton setBackgroundImage:] instead of [deleteButton setImage:] this may allow your text to show (Just a guess, though). – braden Jan 12 '14 at 19:35
  • @Nirav Try using this code: if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationButton"]) { UIButton *deleteButton = (UIButton *)subview; [deleteButton setTitle:@"Close Out" forState:UIControlStateNormal]; [deleteButton setBackgroundColor:[UIColor blackColor]]; for(UIView* view in subview.subviews){ if([view isKindOfClass:[UILabel class]]){ [view removeFromSuperview]; } } } – Guy Kahlon Apr 23 '14 at 08:47
  • Trying to create a non image button with a dark background and light font color. But it is not accepting font properties. If I try to set font size or font name, button does not show any text and shows only plane background if I dont set font properties, its works well – Vaibhav Saran Sep 04 '14 at 06:29
  • What to do for ios 8 .. ? – Stunner Dec 03 '14 at 12:34
  • @Stunner use recursiveDescription (see Vladimirs answer) and find out where Apple moved the view and what structure it has. Post what you find here for others to use. – braden Dec 06 '14 at 05:29
  • anyone figure out ios 8? – josephmisiti Apr 12 '15 at 22:04
3

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

Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • I sheepishly did my own recursion to find out what's in the new ios7 control before I found this question. I wish I would have known about "-recursiveDescription" ... now I know: thanks! – braden Oct 16 '13 at 22:23
0

For iOS 8 or above can use editActionsForRowAtIndexPath method to define the buttons and actions. Refer this link

Community
  • 1
  • 1
Vijay
  • 355
  • 1
  • 5
  • 19