【问题标题】:How button can accept events which is under a UIView?按钮如何接受 UIView 下的事件?
【发布时间】:2016-11-07 04:39:59
【问题描述】:

嗨,在我的应用程序中,我们要求表格视图单元格应该是可编辑的,这意味着可以更改单元格的顺序。为此,我正在使用重新排序控制。随着这个重新排序控制应该覆盖整个单元格,而不仅仅是右端。为此,我正在对显示单元格方法进行以下更改。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

        for (UIView * view in cell.subviews)
        {

            if ([NSStringFromClass([view class]) rangeOfString: @"Reorder"].location != NSNotFound)
            {


                // [view setBackgroundColor:[UIColor redColor]];

                UIView* resizedGripView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
                [resizedGripView setBackgroundColor:[UIColor clearColor]];
                [resizedGripView addSubview:view];
                resizedGripView.backgroundColor=[UIColor clearColor];
                resizedGripView.userInteractionEnabled=YES;

                [cell.contentView addSubview:resizedGripView];

                CGSize sizeDifference = CGSizeMake(resizedGripView.frame.size.width - view.frame.size.width, resizedGripView.frame.size.height - view.frame.size.height);
                CGSize transformRatio = CGSizeMake(resizedGripView.frame.size.width / view.frame.size.width, resizedGripView.frame.size.height / view.frame.size.height);

                //  Original transform
                CGAffineTransform transform = CGAffineTransformIdentity;

                // Scale custom view so grip will fill entire cell

                transform = CGAffineTransformScale(transform, transformRatio.width, transformRatio.height);

                // Move custom view so the grip's top left aligns with the cell's top left

                transform = CGAffineTransformTranslate(transform, -sizeDifference.width / 2.0, -sizeDifference.height / 2.0);

                [resizedGripView setTransform:transform];


                for(UIImageView* cellGrip in view.subviews)
                {
                    if([cellGrip isKindOfClass:[UIImageView class]])
                        [cellGrip setImage:nil];
                }
            }
        }
}

我使用的单元格是自定义单元格,单元格如下所示。

在我对显示单元方法进行了上述更改之后。我可以从任何地方重新排序单元格上的单元格,而不仅仅是从右边缘。正如我们所见,单元格上的删除按钮在此更改后不起作用,因为即使在按钮上也有一个视图被添加到单元格上。单元上可用的任何组件都应该接受事件。请帮助我。

【问题讨论】:

    标签: ios objective-c uitableview custom-cell


    【解决方案1】:

    resizedGripView 添加到cell.contentView 后,您可以编写代码将其他控件置于顶部/前面。试试下面的代码:

    [cell.contentView bringSubviewToFront:cell.btnPrice];  
    [cell.contentView bringSubviewToFront:cell.lblProduct];  
    

    用您的控件替换 btnPricelblProduct

    希望对你有帮助。

    【讨论】:

    • 您好米兰感谢您的回复。但它不起作用:(
    • 嗨,米兰,谢谢,这是一个很大的帮助。非常感谢。
    • 谢谢:)。请问您在ios应用开发方面有多少经验。
    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    相关资源
    最近更新 更多