UITableViewCellDeleteConfirmationControl问题

5

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

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

在iOS 5和6上运行正常。但是在iOS 7上总是返回NO。

有人能告诉我这是iOS 7的问题还是我做错了什么吗?

谢谢!


你好barden,非常感谢你的建议。我已经发布了我的代码,用来修复这个bug。 - V.J
3个回答

27

这是我未完成的粗略实现,供人们参考。

这应该适用于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];
        }

    }
}

我希望这能帮助到某人。


2
在iOS7中,它能够工作是因为使用performSelector:withObject:afterDelay方法将选择器排队到线程的运行循环中,而不是立即执行,从而允许操作系统同时添加删除按钮视图。 - HyBRiD
但在iOS7中,它没有显示我应用于deleteButton的文本。 - Nirav Jain
@Nirav,尝试使用[deleteButton setBackgroundImage:]而不是[deleteButton setImage:],这可能会使您的文本显示出来(只是猜测)。 - braden
试图创建一个非图像按钮,具有深色背景和浅色字体颜色。但是它不接受字体属性。如果我尝试设置字体大小或字体名称,按钮不会显示任何文本,并且仅显示平面背景,如果我不设置字体属性,则它可以正常工作。 - Vaibhav Saran
有人搞定了iOS 8吗? - josephmisiti
显示剩余3条评论

3

如果您在确认按钮上所做的任何操作都依赖于表视图单元格的内部实现细节,而苹果公司可以自由更改它们,因此您的解决方案可能会在任何系统更新后停止工作。在您的情况下,苹果似乎不再在表单元格中使用UITableViewCellDeleteConfirmationControl类。

如果您想在iOS 7上支持您的功能,则需要检查单元格的子视图层次结构在那里如何更改。其中一种可能的方法是在确认按钮可见时记录您单元格上的-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,您可能需要对它们进行微调。


我有点羞涩地自己递归查找新的ios7控件,直到我发现了这个问题。我希望我早知道“-recursiveDescription”...现在我知道了:谢谢! - braden

0

iOS 8或以上版本可以使用editActionsForRowAtIndexPath方法定义按钮和操作。请参考link


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接