example iphone uitableview touchesbegan

iphone - example - uitableview swift 4



El método touchesBegan no se llama en la clase UITableViewController en iPhone (5)

Aquí hay una solución de subclase UITableView que funcionó para mí. Cree una subclase de UITableView y anule hitTest: withEvent: como se muestra a continuación:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { static UIEvent *e = nil; if (e != nil && e == event) { e = nil; return [super hitTest:point withEvent:event]; } e = event; if (event.type == UIEventTypeTouches) { NSSet *touches = [event touchesForView:self]; UITouch *touch = [touches anyObject]; if (touch.phase == UITouchPhaseBegan) { NSLog(@"Touches began"); } } return [super hitTest:point withEvent:event]; }

¿Cómo puedo usar el touchesBegan: withEvent: en la clase UITableViewController?

UITableViewController es una subclase de la clase UIViewController. Entonces, ¿por qué el método no funciona en UITableViewController?


EN SWIFT: Me encontré con esta pregunta mientras buscaba una solución Swift 2. La respuesta publicada por @Steph Sharp me ayudó a resolver el problema en Swift, así que pensé en publicarlo aquí. Aqui tienes:

class CalcOneTableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:") self.view.addGestureRecognizer(tap) }

Función

func handleTap(recognizer: UITapGestureRecognizer) { // Do your thing. }


Tuve un problema similar y encontré un enfoque diferente que no involucra la subclasificación de UITableView. Otra forma de hacer esto es agregar un reconocedor de gestos a la vista de UITableViewController.

Puse este código en el viewDidLoad del UITableViewController:

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

E implementado el controlador de eventos:

- (void)handleTap:(UITapGestureRecognizer *)recognizer { // your code goes here... }

Sé que esta solución no usa touchesBegan, pero descubrí que era una solución simple para el mismo problema.


touchesBegan es también un método UIView además de ser un método UIViewController.

para anularlo, debe subclase UIView o UITableView y no los controladores.


touchesBegan es un método UIView y UITableViewCell en lugar de un método UIViewController y UITableViewController. para que pueda crear la clase personalizada para UITableViewCell, reconozca el evento táctil y toque delegar que funciona para mí.

//TableViewCell.h #import <UIKit/UIKit.h> @class Util; @interface TableViewCell : UITableViewCell { } - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier; @end //TableViewCell.m #import "TableViewCell.h" @implementation TableViewCell -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier format:(NSString*)ec_format{ if (self) { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; } return self; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //you receive touch here NSLog(@"Category Touch %@",self.frame); }

tenga un buen día