How can I tell when something outside of my UITableViewCell was affected?

Like this question I have a custom subclass of UITableViewCell with a UITextField. Its operating mode, with the exception of the keyboard, does not disappear when the user touches another table representing the table or something outside the table. I am trying to find a better place to find out when something outside the cell is touched, then I could call resignFirstResponder in the text box.

If the UITableViewCell can receive touch events for touches outside of its view, then it could just execute a resignFirstResponder, but I see no way to get these events in the cell.

EDIT: I tried this (below) in my subclass of UITableViewCell, but it does not work, I think, because touchhesBegan: withEvent: is not called if the event was handled by the control. I think I need to catch events before they somehow send a chain of defendants.

The solution I'm considering is to add the touchesBegan: withEvent: to method to the view controller. There I can send a resignFirstResponder to all the cells of the table that are visible, except the one in which the sensor was located (let it receive a touch event and process it itself).

Maybe something like this pseudo code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint touchPoint = // TBD - may need translate to cell coordinates for (UITableViewCell* aCell in [theTableView visibleCells]) { if (![aCell pointInside:touchPoint withEvent:event]) { [aCell resignFirstResponder]; } } } 

I am not sure if this is the best way to do this. It seems that some tableviewcell element cannot receive notification of events outside of events.

EDIT2: I thought I had an answer (I even posted it as an answer) using hitTest: withEvent: but that didn't work. It is not always called .: - (

+4
source share
3 answers

[Edited: removed the previous attempt, which does not always work, it does]

OK, I finally decided that the solution works completely. I subclassed the UITableView and redefined the hitTest: withEvent: method. It is called for all touches anywhere in the table view, the only other possible touches are on the navigation bar or on the keyboard, and tableview hitTest does not need to know about it.

This keeps track of the active cell in the table view, and whenever you click on another cell (or non-cell), it sends a resignFirstResponder to a cell that is inactive, giving it the ability to hide its keyboard (or its DatePicker).

 -(UIView*) hitTest:(CGPoint)point withEvent:(UIEvent*)event { // check to see if the hit is in this table view if ([self pointInside:point withEvent:event]) { UITableViewCell* newCell = nil; // hit is in this table view, find out // which cell it is in (if any) for (UITableViewCell* aCell in self.visibleCells) { if ([aCell pointInside:[self convertPoint:point toView:aCell] withEvent:nil]) { newCell = aCell; break; } } // if it touched a different cell, tell the previous cell to resign // this gives it a chance to hide the keyboard or date picker or whatever if (newCell != activeCell) { [activeCell resignFirstResponder]; self.activeCell = newCell; // may be nil } } // return the super hitTest result return [super hitTest:point withEvent:event]; } 

In my UITableViewCell subclasses that have a UITextField, I add the following code to get rid of the keyboard (or pick a date that slides just like a keyboard):

 -(BOOL)resignFirstResponder { [cTextField resignFirstResponder]; return [super resignFirstResponder]; } 

Yay

+9
source

I think you are on the right track, but touchesBegan:withEvent: is the UIResponder method, so you really need to override it in a subclass of UIView, not in a subclass of UIViewController. Your options:

  • If you already subclass UITableViewCell, override touchesBegan:withEvent: there.
  • If you use the standard UITableViewCell, implement tableView:didSelectRowAtIndexPath in your UITableView delta.
0
source

This is a very good solution, the best I have found on the net. The only glitch that I discovered is that if you move from one cell with a text box to another, the keyboard quits and appears again, which leads to disgusting type animation.

0
source

Source: https://habr.com/ru/post/1304196/


All Articles