iosobjective-cuitableview

How do I know when a UITableView loses focus?


I have a UITableView with a UITextField for each row. When the user touches outside the tableview, deselecting the text field, I would like to invoke a method.

However, I don't want to invoke such method if the user selects another row of the table.

Code for cellAtRowForIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    CMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[CMTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    [[cell textField] setTag:[indexPath row]];
    [[cell textField] setDelegate:self];
        
    NSString *tagName = [tagsDisplayName objectAtIndex:[indexPath row]];
    [[cell textField] setText:tagName];
   
    return cell;
}

Solution

  • Something to consider would be using a Tap Gesture recognizer on the view behind the UITableView. You might have to play around with the shouldReceiveTouch event (elaborated on in Gesture recognizer and button actions) to keep the Tap Gesture Recognizer from firing when you click somewhere in the UITableView.