【问题标题】:How to locate the layout across view hierarchy?如何跨视图层次结构定位布局?
【发布时间】:2018-01-19 12:54:49
【问题描述】:

我在列表视图控制器中有一个 menuView。当单元格中的更多按钮被录制时,在 UITableViewCell 上添加的 menuView。 我用单例实现了效果。

@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,它变成了这样:

那么问题来了。

我想把menuView放到控制器的view上,因为它是唯一的或者隐藏的,以前是属于cell的。

如何将选择的更多按钮的布局转换为控制器的视图? 如何使用计算方法?

- 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) {    }];
}

如何更自适应地实现?

【问题讨论】:

  • 你想在点击更多按钮时显示menuView并且在表格滚动时不应该隐藏?
  • 没有。使用UIScollViewDelegate 即可。

标签: ios objective-c uitableview layout frame


【解决方案1】:

也许你可以这样尝试:

// 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;
}

【讨论】:

【解决方案2】:

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

 cell.cellIndexpath  = indexpath

【讨论】:

    【解决方案3】:

    看看UIPopoverPresnetationController 看看它是否能胜任这项工作。它应该可以在 iPhone 上使用。将menu view 放在controller’view 上会导致滚动表格视图时出现问题


    使用

    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setTargetRect:self.detaiLabel.frame inView:self];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      相关资源
      最近更新 更多