Touch events on UITableView?

前端 未结 7 643
[愿得一人]
[愿得一人] 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:33

    You should forward the touch event to the view's controller. Subclass your tableview control and then override the method:

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

    then , you can do what you want in the controller's touch methods.

    0 讨论(0)
  • 2020-12-23 10:33

    In your controller class declare a method which removes the bottom view. Something like this:

    -(IBAction)bottomViewRemove:(id)sender {
    
    [bottomView removeFromSuperview];
    
    }
    

    In Interface Builder, select your view and in the identity inspector in the custom class section, change the class from UIView to UIControl. After that go to the connections inspector and connect the TouchUpInside event to the method declared above. Hope this helps.

    0 讨论(0)
  • 2020-12-23 10:34

    To receive touch events on the UITableView use:

    - (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];
    }
    
    0 讨论(0)
  • 2020-12-23 10:35

    I was facing the problem since a long time and didn't got any working solution. Finally I choose to go with a alternative. I know technically this is not the solution but this may help someone looking for the same for sure.

    In my case I want to select a row that will show some option after that I touch anywhere on table or View I want to hide those options or do any task except the row selected previously for that I did following:

    1. Set touch events for the view. This will do the task when you touch anywhere on the view except the table view.

    2. TableView's didSelectRowAtIndexPath do following

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

    I know that this is older post and not a good technical solution but this worked for me. I am posting this answer because this may help someone for sure.

    Note: The code written here may have spell mistakes because directly typed here. :)

    0 讨论(0)
  • 2020-12-23 10:37

    I just stumbled onto what may be a solution for your problem. Use this code when you create your table view:

    tableView.canCancelContentTouches = NO;
    

    Without setting this to NO, the touch events are cancelled as soon as there is even a slight bit of vertical movement in your table view (if you put NSLog statements in your code, you'll see that touchesCancelled is called as soon as the table starts scrolling vertically).

    0 讨论(0)
  • 2020-12-23 10:43

    Try this methods:

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

    Use scrollViewDidEndDragging like alternative of touchesEnded. Hope it helps.

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