Ios 如何跨视图层次定位布局?

Ios 如何跨视图层次定位布局?,ios,objective-c,uitableview,layout,frame,Ios,Objective C,Uitableview,Layout,Frame,我在列表视图控制器中有一个菜单视图。录制单元格中的“更多”按钮时,在UITableViewCell上添加的菜单视图。 我用singleton实现了这个效果 @implementation ProductsOperationMenu static ProductsOperationMenu *_instance; + (instancetype)sharedInstance{ static dispatch_once_t onceToken; dispatch_once(&

我在列表视图控制器中有一个菜单视图。录制单元格中的“更多”按钮时,在UITableViewCell上添加的菜单视图。 我用singleton实现了这个效果

@implementation ProductsOperationMenu
static ProductsOperationMenu *_instance;
+ (instancetype)sharedInstance{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] initWithFrame:CGRectZero];
    });
    return _instance;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}
ZBMyProductsCell.m

@implementation ZBMyProductsCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    _operationMenu = [[ProductsOperationMenu alloc] initWithFrame: CGRectZero];
}

- (IBAction)operationButtonClick:(UIButton *)sender {
    if ([self.contentView.subviews containsObject:_operationMenu]) {
        _operationMenu.hidden = ![_operationMenu isHidden];
    } else{
        [self.contentView addSubview:_operationMenu];
        _operationMenu.hidden = NO;
    }

    [_operationMenu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(205);
        make.height.mas_equalTo(60);
        make.bottom.mas_equalTo(self.operationButton).offset(0);
        make.right.mas_equalTo(self.operationButton.mas_left).offset(-10);
    }];
}
没有Singleton,它变成了这样:

所以问题来了

我想把菜单视图放在控制器的视图上,因为它是唯一的或隐藏的,过去它属于单元格

如何将所选“更多”按钮的布局转换为控制器的视图? 如何使用这些方法进行计算

- convertPoint:toView:
- convertPoint:fromView:
......
我用了一种简单的方法。代码如下:

- (void)clickOperationButtonOfProductsCell:(ZBMyProductsCell *)myProductsCell{
    NSUInteger * operationIndex = [self.myProductsTableView.visibleCells indexOfObject:myProductsCell];
    CGFloat originY = operationIndex.row * 110 + 50 + 40;
    CGRect originFrame = CGRectMake(KScreenWidth - 55, originY, 0, 60);
    CGRect finalFrame = CGRectMake(KScreenWidth - 260, originY, 205, 60);
    self.operationMenuView.frame = originFrame;
    [UIView animateWithDuration: 0.5 delay: 0 options: UIViewAnimationOptionCurveEaseIn animations:^{
        self.operationMenuView.frame = finalFrame;
    } completion:^(BOOL finished) {    }];
}

如何更自适应地实现它?

在每个单元模型中创建一个名为cellIndexpath的变量,并在cellForRow init中创建它

 cell.cellIndexpath  = indexpath

也许你可以这样试试:

// here is your cell where the more button belongs to
@interface ZBMyProductsCell: UITableViewCell

@property (nonatomic, copy) void(^moreAction)(ZBMyProductsCell *cell);

@end

@implementation ZBMyProductsCell

- (void)_moreButtonClicked:(UIButton *)sender {
    !self.moreAction ?: self.moreAction(self);
}

@end

// here is your view controller where your table view belongs to
// this is a `UITableViewDataSource` delegate method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZBMyProductsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZBMyProductsCell" forIndexPath:indexPath];

    // Configure the cell...
    cell.moreAction = ^(ZBMyProductsCell *cell) {
        CGRect rect = [tableView rectForRowAtIndexPath:indexPath];
        // write your own code to show/hide the menu
    };

    return cell;
}

查看
UIPopOverpreNetationController
,看看它是否可以完成此任务。它应该可以在iPhone上使用。当您滚动表格视图时,将
菜单视图
放在
控制器视图上
将导致问题


使用


是否要在单击“更多”按钮时显示菜单视图,并且在表格滚动时不应隐藏该菜单视图?否。使用
UIScollViewDelegate
是可以的。答案不是预期的。也许你可以检查一下这个问题
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:self.detaiLabel.frame inView:self];