5

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

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

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

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

谢谢!

4

3 回答 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];
        }

    }
}

我希望这可以帮助别人。

于 2013-10-16T19:50:56.033 回答
3

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

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

<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,您可能需要调整它们

于 2013-10-03T12:59:44.423 回答
0

对于 iOS 8 或更高版本可以使用editActionsForRowAtIndexPath方法来定义按钮和动作。参考这个链接

于 2016-05-10T18:24:25.680 回答