6

didSelectCellAtIndexPath:そのセルのアクセサリ ビューにある UISwitch を使用する機能を保持しながら、UITableViewCell がデリゲートをトリガーするのを無効にする方法はありますか。

cell.userInteractionEnabled = NO を設定できることはわかっています。これにより、セルが無効になりますが、アクセサリ ビューでスイッチを使用できなくなります。メソッドでどのセルがタップされたかを検出しようとすることもできdidSelectCellAtIndexPath:ますが、テーブル ビューは動的であり、ユーザーの操作に応じて変化するため、面倒になる可能性があります。

使用できるシンプルでエレガントなソリューションを探しています。何か案は?

4

2 に答える 2

0

UISwitchに依存するのではなく、イベント リスナーを直接 に追加しますdidSelectRowAtIndexPath

-(void)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    // Here I assume you created a subclass MyCell of UITableViewCell
    // And exposed a member named 'switch' that points to your UISwitch

    MyCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if (cell == nil) {
         cell = [[MyCell alloc] init];
         cell.selectionStyle = UITableViewCellSelectionStyleNone; // This could be moved into MyCell class
         [cell.switch addTarget:self action:@selector(switchChanged:) forControlEvent:UIControlEventValueChanged];
    }

    // Now we need some way to know which cell is associated with the switch
    cell.switch.tag = indexPath.row;
}

スイッチイベントをリッスンするには、このメソッドを同じクラスに追加します

-(void)switchChanged:(UISwitch*)switch {
    NSUInteger cellIndex = switch.tag;
    ...
}
于 2013-06-23T02:32:19.907 に答える