質問

私は持っている UIViewControllerUITableView ビューの子供として、私がやりたいのは、列に触れると、下にビューを表示していることです。ユーザーが他の場所に触れてから列またはボトムビューに触れた場合、そのビューを隠したいと思います。

問題は、クリックしたときです UITableView 発砲しません touchesEnded イベント。

次に、どうすればタッチを検出できますか UITableView 行選択イベントで区別します。

ありがとう。

役に立ちましたか?

解決 4

私は長い間問題に直面していましたが、うまくいきませんでした。最後に、代替手段を使用することを選択します。私は技術的にこれが解決策ではないことを知っていますが、これは確かに同じことを探している人を助けるかもしれません。

私の場合、私はその後、いくつかのオプションを表示する行を選択したいのですが、テーブルのどこにでもタッチするか、それらのオプションを非表示にするか、以前に選択した行を除いてタスクを実行します。

  1. ビュー用のタッチイベントを設定します。これは、テーブルビューを除くビューのどこにでもタッチするときにタスクを実行します。

  2. TableViewのDidSeLectrowatindexpathは次のとおりです

     - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
        if(indexPath.row != previousSelectedRow && previousSelectedRow != -1) {
            // hide options or whatever you want to do
            previousSelectedRow = -1;
        }
        else {
            // show your options or other things
            previousSelectedRow = indexPath.row;
        }
     }
    

これは古い投稿であり、優れた技術的なソリューションではないことを知っていますが、これは私にとってうまくいきました。私はこの答えを投稿しています。なぜなら、これは確かに誰かを助けるかもしれないからです。

注:ここに書かれたコードには、ここで直接入力されているため、呪文の間違いがある場合があります。 :)

他のヒント

何もサブクラス化する必要はありません、あなたは追加することができます UITapGestureRecognizerUITableView 基準に応じてジェスチャーを吸収するかどうか。

viewdidloadで:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapOnTableView:)];
[self.myTableView addGestureRecognizer:tap];

次に、基準のためにこのようなアクションを実装します。

-(void) didTapOnTableView:(UIGestureRecognizer*) recognizer {
    CGPoint tapLocation = [recognizer locationInView:self.myTableView];
    NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:tapLocation];

    if (indexPath) { //we are in a tableview cell, let the gesture be handled by the view
        recognizer.cancelsTouchesInView = NO;
    } else { // anywhere else, do what is needed for your case
        [self.navigationController popViewControllerAnimated:YES];
    }
}

また、テーブルのどこにでもクリックしたいだけでなく、セル行のボタンではクリックしない場合は、上記の最初のコードフラグメントのみを使用する必要があることに注意してください。典型的な例は、uitableviewがあり、Uisearchbarもある場合です。ユーザーがクリックしたり、スクロールしたり、テーブルビューを見るときに検索バーを排除する必要があります。コードの例...

-(void)viewDidLoad {
    [super viewDidLoad];
    etc ...

    [self _prepareTable];
}
-(void)_prepareTable {
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.allowsSelection = NO;
    etc...

    UITapGestureRecognizer *anyTouch =
        [[UITapGestureRecognizer alloc]
         initWithTarget:self action:@selector(tableTap)];
    [self.tableView addGestureRecognizer:anyTouch];
}
// Always drop the keyboard when the user taps on the table:
// This will correctly NOT affect any buttons in cell rows:
-(void)tableTap {
    [self.searchBar resignFirstResponder];
}
// You probably also want to drop the keyboard when the user is
// scrolling around looking at the table. If so:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.searchBar resignFirstResponder];
}
// Finally you may or may not want to drop the keyboard when
// a button in one cell row is clicked. If so:
-(void)clickedSomeCellButton... {
    [self.searchBar resignFirstResponder];
    ...
}

それが誰かを助けることを願っています。

タッチイベントをビューのコントローラーに転送する必要があります。 TableViewコントロールをサブクラス化してから、メソッドをオーバーライドします。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];  //let the tableview handle cell selection 
    [self.nextResponder touchesBegan:touches withEvent:event]; // give the controller a chance for handling touch events 
}

次に、コントローラーのタッチメソッドでやりたいことを行うことができます。

私はあなたの問題の解決策であるかもしれないものにつまずいただけです。テーブルビューを作成するときにこのコードを使用します。

tableView.canCancelContentTouches = NO;

これを設定せずに NO, 、タッチイベントは、テーブルビューに少し垂直な動きがあるとすぐにキャンセルされます(コードにnslogステートメントを入れた場合、あなたはそれがわかります touchesCancelled テーブルが垂直にスクロールを開始するとすぐに呼び出されます)。

この方法を試してください:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{

}

TouchSedendedの代替品のようにScrollViewDidendDraggingを使用します。それが役に立てば幸い。

のタッチイベントを受信します UITableView 使用する:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  //<my stuff>

  [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   //<my stuff>

   [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
  //<my stuff>

  [super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
{
   //<my stuff>
   [super touchesCancelled:touches withEvent:event];
}

uitableviewでタッチイベントを受け取ります

コントローラークラスでは、ボトムビューを削除するメソッドを宣言します。このようなもの:

-(IBAction)bottomViewRemove:(id)sender {

[bottomView removeFromSuperview];

}

インターフェイスビルダーで、ビューを選択し、カスタムクラスセクションのIDインスペクターで、クラスをから変更します。 UIViewUIControl. 。その後、Connections Inspectorに移動し、Touchupinsideイベントを上記の方法に接続します。お役に立てれば。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top