Touch events on UITableView?

前端 未结 7 644
[愿得一人]
[愿得一人] 2020-12-23 10:26

I have UIViewControllerand UITableView as child in the view, what I want to do is when I touch any row I am displaying a view at bottom. I want to

相关标签:
7条回答
  • 2020-12-23 10:44

    No need to subclass anything, you can add a UITapGestureRecognizer to the UITableView and absorb the gesture or not depending on your criteria.

    In your viewDidLoad:

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

    Then, implement your action like this for the criteria:

    -(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];
        }
    }
    

    And note that if you just want to simply pick up clicks anywhere on the table, but not on any buttons in cell rows, you need only use the first code fragment above. A typical example is when you have a UITableView and there is also a UISearchBar. You want to eliminate the search bar when the user clicks, scrolls, etc the table view. Code example...

    -(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];
        ...
    }
    

    Hope it helps someone.

    0 讨论(0)
提交回复
热议问题