Search code examples
iosuitableviewswiftuievent

Event handling in tableView


What I'm trying to do is handling touch events so that when i screen is getting touched, i want to take some action. For example changing background color The things i tried: // I subclassed table view controller

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
     tableView.backgroundColor = UIColor.orangeColor()
}

that didn't work, i suspected that tvc may not be first responder so that table view handles the touch events. So i tried:

 override func viewDidLoad() {
     super.viewDidLoad()
     tableView.resignFirstResponder()    
}

Also tried:

override func becomeFirstResponder() -> Bool {
    return true
}

override func canBecomeFirstResponder() -> Bool {
    return true
}

None of them work. How can i handle events ? What I'm missing?

EDIT

The selected answer in terms of native swift code:

override func viewDidLoad() {
        super.viewDidLoad()

        var tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tap:")
        tapGestureRecognizer.cancelsTouchesInView = true
        self.tableView.addGestureRecognizer(tapGestureRecognizer)




    }

    func tap(recognizer: UITapGestureRecognizer) {
        if recognizer.state == UIGestureRecognizerState.Ended {
            var tapLocation  = recognizer.locationInView(self.tableView)
            var tapIndexPath : NSIndexPath? = self.tableView.indexPathForRowAtPoint(tapLocation)

            if let index = tapIndexPath  {

                self.tableView(self.tableView, didSelectRowAtIndexPath: index)
            } else {
                self.tableView.backgroundColor = UIColor.orangeColor()
            }
        }

    }

Solution

  • If you want to react to touches all over the view, not just the cells, add a tap gesture recognizer in viewDidLoad:

    - (void)addTapGestureForListTable {
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTappedOnView:)];
        tapGestureRecognizer.cancelsTouchesInView = YES;
        [self.tableView addGestureRecognizer:tapGestureRecognizer];
    }
    

    And then implement the method userTappedOnView. If you want to distinguish between touches on cells or not, implement it like so:

    - (void)userTappedOnView:(UITapGestureRecognizer *)recognizer {
        if (recognizer.state == UIGestureRecognizerStateEnded) {
            CGPoint tapLocation = [recognizer locationInView:self.tableView];
            NSIndexPath *tapIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
            if (tapIndexPath) {
                [self tableView:self.tableView didSelectRowAtIndexPath:tapIndexPath];
            }
        }
    }
    

    If you want to react to touches on a cell, you have to make your tableView's delegate point to the controller. In viewDidLoad do:

    self.tableView.delegate = self;
    

    And then implement the method

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;