繁体   English   中英

仅在标题中完成才触发UIButton内部事件触发

[英]UIButton touch up inside event only fired if done in title

我正在自定义具有几个子视图的UITableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if (self) {
    _mainView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)];

    _hiddenOptionsView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)];

    _menuView = [[CellMenuView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 20.0f)];

    [self addSubview:_menuView];
    [self addSubview:_hiddenOptionsView];
    [self addSubview:_mainView];
  }
  return self;
}

CellMenuView类是一个UIView子类,它具有两个UIButtons并在初始化时设置了对应的目标动作:

- (id)initWithFrame:(CGRect)frame {   
  self = [super initWithFrame:frame];
  if (self) {
    UIImageView *background = [[UIImageView alloc] initWithFrame:frame];
    [background setImage:[UIImage imageNamed:@"cell_menu_bg.png"]];
    [self addSubview:background];

    CGFloat buttonX = frame.origin.x;
    CGFloat buttonY = frame.origin.y + 3.0f;
    CGFloat buttonWidth = frame.size.width / 2.0f;
    CGFloat buttonHeight = 10.0f;

    _editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _editButton.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
    _editButton.backgroundColor = [UIColor clearColor];
    _editButton.titleLabel.shadowColor = [UIColor blackColor];
    _editButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.5f);
    _editButton.titleLabel.font = [UIFont boldSystemFontOfSize:22.0f];
    [_editButton setTitle:@"Edit" forState:UIControlStateNormal];
    [_editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_editButton addTarget:self action:@selector(editButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_editButton];

    _fireButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _fireButton.frame = CGRectMake(buttonWidth, buttonY, buttonWidth, buttonHeight);
    _fireButton.backgroundColor = [UIColor clearColor];
    _fireButton.titleLabel.shadowColor = [UIColor blackColor];
    _fireButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.5f);
    _fireButton.titleLabel.font = [UIFont boldSystemFontOfSize:22.0f];
    [_fireButton setTitle:@"Fire" forState:UIControlStateNormal];
    [_fireButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_fireButton addTarget:self action:@selector(fireButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_fireButton];

    return self;
  }
}

我已经实现了目标行动的方法,但他们没有被执行。 相反,每次我按下按钮之一时, didSelectRowAtIndexPath:优先于按钮内的didSelectRowAtIndexPath:事件。

我能够在按钮上触发事件的唯一方法是确保触摸UIButton's标题中的字母之一(当然是使用模拟器)。

我必须说, _menuView隐藏在其他子视图的后面,并在按下单元格中的自定义附件按钮时显示在单元格下方。 修改Y原点会显示它,而再次将其设置为0.0f消失。

我认为这可能与视图层次结构有关,因为过去通过将按钮直接添加到单元格没有任何问题。 但是我只是在猜测。

如何使按钮上的事件优先于didSelectRowAtIndexPath:方法?

谢谢!

实行

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

然后为您不想选择的indexPath返回nil; 这样可以防止在该单元格上调用didSelectRowAtIndexPath ,并且(希望)将调用您的自定义操作。

更新:我自己实现了此操作(在按钮上添加了边框),这就是我所拥有的。 在此处输入图片说明

实现此目的的代码(与您的不同)如下:

CellMenuView.m

    self.backgroundColor = [UIColor blackColor];
    _editButton.layer.borderColor = [UIColor whiteColor].CGColor;
    _editButton.layer.borderWidth = 2;
    _fireButton.layer.borderColor = [UIColor whiteColor].CGColor;
    _fireButton.layer.borderWidth = 2;

UITableViewCell子类

    // Change in height to make it taller
    _menuView = [[TestBtn alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 320.0f, 60.0f)];

UITableViewController子类

    self.tableView.rowHeight = 100;

经过测试,我的工作正常。 如果我在白方块内单击, didSelectRowAtIndexPath常规执行按钮操作,并且不会调用didSelectRowAtIndexPath 仅当我在按钮外部单击时才调用didSelectRowAtIndexPath

我认为这里的问题可能是您的按钮高度/框架以及行高。 在按钮上添加边框,以检查其可单击区域,并增加按钮的高度以增加可单击区域。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM