UITableViewCellDeleteConfirmationControl问题

时间:2013-10-03 12:39:41

标签: ios

我在项目中使用以下代码:

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

这在iOS 5和6上运行良好。 但是在iOS 7上它总是返回NO。

任何人都可以告诉我这是iOS 7的问题还是我做错了什么?

谢谢!

3 个答案:

答案 0 :(得分:27)

这是我为人们构建的hackish未完成的实现。

这适用于iOS6--和iOS7。

显然,此代码包含在您的子类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];
        }

    }
}

我希望这有助于某人。

答案 1 :(得分:3)

无论您使用该确认按钮做什么,都依赖于Apple视图单元的内部实现细节,Apple可以自由更改,并且您的解决方案可能会在任何系统更新后停止工作。在你的情况下,Apple似乎不再在表格单元格中使用UITableViewCellDeleteConfirmationControl类。

如果您想在iOS 7上支持您的功能,您需要检查单元格的子视图层次结构是如何更改的。当确认按钮可见时,可能的方法之一可能是在您的单元格上使用log -recursiveDescription方法,您将看到类似的结构(我从日志中删除了一些信息):

<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)> 

如您所见,现在有两个私有类处理确认UI - UITableViewCellDeleteConfirmationView和UITableViewCellDeleteConfirmationButton,您可能需要调整它们

答案 2 :(得分:0)

对于iOS 8或更高版本,可以使用editActionsForRowAtIndexPath方法定义按钮和操作。请参阅此link