ios - Event handling in tableView -


what i'm trying handling touch events when screen getting touched, want take action. example changing background color things tried: // subclassed table view controller

override func touchesended(touches: nsset, withevent event: uievent) {      tableview.backgroundcolor = uicolor.orangecolor() } 

that didn't work, suspected tvc may not first responder table view handles touch events. 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 handle events ? 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()             }         }      } 

if want react touches on view, not cells, add tap gesture recognizer in viewdidload:

- (void)addtapgestureforlisttable {     uitapgesturerecognizer *tapgesturerecognizer = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(usertappedonview:)];     tapgesturerecognizer.cancelstouchesinview = yes;     [self.tableview addgesturerecognizer:tapgesturerecognizer]; } 

and implement method usertappedonview. if want distinguish between touches on cells or not, implement 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 want react touches on cell, have make tableview's delegate point controller. in viewdidload do:

self.tableview.delegate = self; 

and implement method

- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath; 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -