0

私は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は、初期化時に対応するターゲット アクションが設定されたUIView2 つのサブクラスです。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タイトルの文字の1つがタッチされていることを確認することです(もちろん、シミュレーターを使用しています)。

_menuView他のサブビューの背後に隠れており、セル内のカスタム アクセサリ ボタンが押されたときにセルの下に表示されると言わざるを得ません。Y 原点を変更すると表示され、0.0f再度設定すると消えます。

セルに直接ボタンを追加して過去に問題が発生したことがないため、ビュー階層に関連している可能性があると思います。しかし、私はここで推測しています。

didSelectRowAtIndexPath:ボタンのイベントをメソッドよりも優先させるにはどうすればよいですか?

ありがとう!

4

1 に答える 1

0

埋め込む

- (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ボタンの外側をクリックしたときにのみ呼び出されます。

ここでの問題は、ボタンの高さ/フレームと行の高さにあると思います。ボタンに境界線を追加してクリック可能な領域を確認し、ボタンの高さを増やしてクリック可能な領域を増やします。

于 2012-11-02T09:42:43.500 に答える