如果在标题中完成,UIButton触摸内部事件仅触发

时间:2012-11-02 09:37:56

标签: objective-c ios uitableview uibutton

我正在使用多个子视图自定义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:优先于按钮内的触摸事件。

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

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

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

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

谢谢!

1 个答案:

答案 0 :(得分:0)

实施

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

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

更新:在我自己实现此功能后(为按钮添加边框),这就是我所拥有的。enter image description here

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

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

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