1

I have two UITextFields on each UITableViewRow, and for some reason when I press return on the UIKeyboard one particular rows neither of the two UITextFields will reaspond (the cursor is not visible).

I have a custom UITableViewCell that I am using, I can show you the code for this if you like however I dont think that is the problem as the return key works for 95% of the UITableViewCells. So I was thinking maybe it was how I was handling the delegate methods for the UITextFields?

This is the code I am using for the delegate methods.

-(BOOL)textFieldShouldBeginEditing:(UITextField*)textfield {

        int height = self.finishingTableView.frame.size.height;
         self.finishingTableView.frame= CGRectMake(self.finishingTableView.frame.origin.x, self.finishingTableView.frame.origin.y, self.finishingTableView.frame.size.width, 307);

    // select correct row
    if (textfield.tag > 999999) {
        int adjustTag = textfield.tag-1000000; // remove a million so that you have the text fields correct position in the table. (this is only for height textfields)
        NSIndexPath *indexPath =[NSIndexPath indexPathForRow:adjustTag inSection:0];
        [finishingTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        [self tableView:finishingTableView didSelectRowAtIndexPath:indexPath];
        [self.finishingTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
        return YES;
    } else {
        NSIndexPath *indexPath =[NSIndexPath indexPathForRow:textfield.tag inSection:0];
        [finishingTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        [self tableView:finishingTableView didSelectRowAtIndexPath:indexPath];
        [self.finishingTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
        return YES;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"%i", textField.tag+1);

    [[self.view viewWithTag:textField.tag+1] becomeFirstResponder];
    // this means there has been a change in the UItextfield


    NSLog(@"%@", selectedItemDictionary);
    if (textField.tag < 999999) {
        tempFinishingObjectDictionary = [selectedItemDictionary mutableCopy];
        if (![textField.text isEqualToString:[selectedItemDictionary objectForKey:@"mMM"]]) {
            tempUpdatedRow = @"T";
            // remove kevalues
            [tempFinishingObjectDictionary removeObjectForKey:@"updatedRow"];
            [tempFinishingObjectDictionary removeObjectForKey:@"new_mMM"];
            // update keyvalues
            [tempFinishingObjectDictionary setValue:tempUpdatedRow forKey:@"updatedRow"];
            [tempFinishingObjectDictionary setValue:textField.text forKey:@"new_mMM"];
        }
    } else {
        if (![textField.text isEqualToString:[selectedItemDictionary objectForKey:@"hMM"]]) {
            tempUpdatedRow = @"T";
            // remove kevalues
            [tempFinishingObjectDictionary removeObjectForKey:@"updatedRow"];
            [tempFinishingObjectDictionary removeObjectForKey:@"new_hMM"];
            // update keyvalues
            [tempFinishingObjectDictionary setValue:tempUpdatedRow forKey:@"updatedRow"];
            [tempFinishingObjectDictionary setValue:textField.text forKey:@"new_hMM"];
        }
    }
    NSLog(@"%@", tempFinishingObjectDictionary);
    [coreDataController editSelectedFinishing:htmlProjID UpdatedNSD:tempFinishingObjectDictionary SelectedRow:selectedItemIndexPathRow];
    [SVProgressHUD dismiss];


    NSLog(@"%@", selectedItemDictionary);

    return YES;
}

I don't have any idea on where to look or how to find this error as it seems so random, but happens on the same UITextFields every time.

The code above is where I think the problem could lie; however, having logged everything and debugged for several hours, I am starting to think it's a bug with UITextFields in UITableViewCells.

halfer
  • 19,824
  • 17
  • 99
  • 186
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

1 Answers1

2

is [[self.view viewWithTag:textField.tag+1] canBecomeFirstResponder] == YES?

also you need to resign the current UITextField before setting the next as the first responder

[textField resignFirstResponder];
Mike S
  • 11,329
  • 6
  • 41
  • 76
Raymond Brion
  • 332
  • 1
  • 10
  • I just tried adding resignFirstResponder above where I set textfield.tag+1 becomeFirstResponder. It removed the UIKeyboard from view and still did not make the textfield.tag+1 becomeFirstReasponder. – HurkNburkS Jan 04 '14 at 03:03
  • Can you check/log if this is the result? [[self.view viewWithTag:textField.tag+1] canBecomeFirstResponder] == YES – Raymond Brion Jan 04 '14 at 03:08
  • just trying to figure out how to do that and will let you know asap... just not sure how to log canbecomefirstreasponder – HurkNburkS Jan 04 '14 at 03:11
  • Something like this NSLog([[self.view viewWithTag:textField.tag+1] canBecomeFirstResponder] ? @"Yes" : @"No"); – Raymond Brion Jan 04 '14 at 03:13
  • yep i figured out how to log. and that UItextfield returns NO – HurkNburkS Jan 04 '14 at 03:14
  • Thats the problem I think. I have encountered this problem before. Where my textfield was textfield.enabled = NO or textfield.userInteractionEnabled = NO – Raymond Brion Jan 04 '14 at 03:15
  • what is intresting is that when i press return on the cell before textFieldShouldBeginEditing is called twice.. unlike the other cells that work they only get called once.. So I will look down this route and let you know how I go.. other than that do you think the problem lies with resginFirstResponder not being called? if so why would other textfields work but not the occasian one like I am experianceing? – HurkNburkS Jan 04 '14 at 03:17
  • I believe the problem lies on the [[self.view viewWithTag:textField.tag+1] canBecomeFirstResponder] returning a NO and not on the resigningFirstResponder. – Raymond Brion Jan 04 '14 at 03:19
  • You said that 95% of the time it works fine. I am very suspicious that the UITextField you're trying to assign first responder to as actually nil at the time. For example, if you hit return on your cells until you get to the bottom of the screen, and the NEXT cell is off the screen then its UITextField will not have been instantiated wit the expected tag yet. Can you give specific details about when it occurs or screenshots to help shed light on the precise cause? – Mike S Jan 04 '14 at 03:25
  • Okay, I have an idea I will look into the nil value first and get back to you. I only say that as I am unable to give you the specifics you request, because it happens only on certian tableviewcells. so each time it will happen on the same UItableViewCell – HurkNburkS Jan 04 '14 at 03:34
  • Also see this answer http://stackoverflow.com/a/9195235/1241782 to implement that auto-scrolling BEFORE assigning first responder, in the code in your question you're doing the auto-scrolling in testFieldShouldBeginEditing which is after you assign first responder. You would need to tweak it to work for 2 UITextFields per cell though. – Mike S Jan 04 '14 at 03:36
  • okay perfect thankyou.. that was the next problem I was planning on working on haha... anyway ill let you know how I go with the current issue, Im just checking over where I initalize it and making sure that all works correctly. – HurkNburkS Jan 04 '14 at 03:37
  • still working on it.. having abit of a battle with this whole cell being nil or what ever.. I have been trhough the link you supplied but still having troubl. – HurkNburkS Jan 04 '14 at 23:46
  • I have accepted this question as it helped me find the problem.. however I am still yet to solve the problem.... – HurkNburkS Jan 06 '14 at 17:37