简体   繁体   English

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

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

I am customizing a UITableViewCell with several subviews: 我正在自定义具有几个子视图的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;
}

The class CellMenuView is a UIView subclass which has two UIButtons with their counterpart target actions setup on initialization: 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;
  }
}

I have implemented the target action methods but they aren't being executed. 我已经实现了目标行动的方法,但他们没有被执行。 Instead, didSelectRowAtIndexPath: takes precedence over the touch up events inside buttons every time I pressed one of them. 相反,每次我按下按钮之一时, didSelectRowAtIndexPath:优先于按钮内的didSelectRowAtIndexPath:事件。

The only way I have been able to fire the events on buttons is making sure that one of the letters in UIButton's title is touched (using the simulator, of course). 我能够在按钮上触发事件的唯一方法是确保触摸UIButton's标题中的字母之一(当然是使用模拟器)。

I must say that _menuView is hidden behind the other subviews and shown below the cell when a custom accessory button in cell is pressed. 我必须说, _menuView隐藏在其他子视图的后面,并在按下单元格中的自定义附件按钮时显示在单元格下方。 It appears by modifying the Y origin and disappears by setting it to 0.0f again. 修改Y原点会显示它,而再次将其设置为0.0f消失。

I think it may be related to the view hierarchy, because I haven't had any problems in the past by adding buttons directly to the cell. 我认为这可能与视图层次结构有关,因为过去通过将按钮直接添加到单元格没有任何问题。 But I'm just guessing here. 但是我只是在猜测。

How can I make the events on buttons take precedence over the didSelectRowAtIndexPath: method? 如何使按钮上的事件优先于didSelectRowAtIndexPath:方法?

Thanks! 谢谢!

Implement 实行

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

And return nil for the indexPath you don't want selected; 然后为您不想选择的indexPath返回nil; this will prevent didSelectRowAtIndexPath from being called on that cell, and (hopefully), your custom action will be called. 这样可以防止在该单元格上调用didSelectRowAtIndexPath ,并且(希望)将调用您的自定义操作。

Update: After implementing this myself (adding borders to the buttons), this is what I have. 更新:我自己实现了此操作(在按钮上添加了边框),这就是我所拥有的。 在此处输入图片说明

The code to achieve this (which is different than yours), is as follows: 实现此目的的代码(与您的不同)如下:

CellMenuView.m 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 subclass UITableViewCell子类

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

UITableViewController subclass UITableViewController子类

    self.tableView.rowHeight = 100;

After testing, mine works as expected. 经过测试,我的工作正常。 If I click within the white squares, the button action is performed as normal, and didSelectRowAtIndexPath is not called. 如果我在白方块内单击, didSelectRowAtIndexPath常规执行按钮操作,并且不会调用didSelectRowAtIndexPath didSelectRowAtIndexPath is only called when I click outside of the buttons. 仅当我在按钮外部单击时才调用didSelectRowAtIndexPath

I think the issue here might be your button heights/frames, as well as your row height. 我认为这里的问题可能是您的按钮高度/框架以及行高。 Add borders to your buttons, to check its clickable area, and increase the height of the button to increase the clickable area. 在按钮上添加边框,以检查其可单击区域,并增加按钮的高度以增加可单击区域。

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

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