9

Im getting a crash, when using a UItextField, inside my customCell, and when i resignFirstResponder the textfield, but its not visible anymore(the table view scrolled out of window). I still can find the textfield, the pointer continues accessible, it is no null, and the crash only occurs on IOS7, on IOS6 i dont have this problem. Heres some code :

The textField is a global variable.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];

    TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[TableCell alloc] init];

        if(indexPath.row == 0)
        {
            [textField setFrame:CGRectMake(15, 5, cell.frame.size.width-60, cell.frame.size.height)];
            textField.textAlignment = NSTextAlignmentLeft;
            [textField setBorderStyle:UITextBorderStyleNone];
            textField.textColor = [UIColor blackColor];
            textField.tag = indexPath.row;
            textField.delegate = self;
            textField.secureTextEntry = YES;
            [textField setFont:[UIFont fontWithName:@"Arial-BoldMT" size:15]];
            textField.textColor = [UIColor whiteColor];
            textField.returnKeyType = UIReturnKeyDone;
            [textField setAdjustsFontSizeToFitWidth:YES];
            textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Senha" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
            [cell.contentView textField];
        }
}
    return cell;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//    NSLog(@"text field %@",textField);
//    NSLog(@"tfield return: %d",textField.isFirstResponder);
    [textField resignFirstResponder];
//    [self.view endEditing:YES];

    return NO;
}
darkman
  • 993
  • 3
  • 13
  • 31
  • You have the same textField instance in all UITableViewCells ? It's strange. How to alloc init the textField IVar ? – tdelepine Oct 01 '13 at 13:46
  • i just put one TextField for example but i have : if(indexPath.row == 0) { [textField1 setFrame:CGREctMake(0,0,0,0)]; } if(indexPath.row == 1) { [textField2 setFrame:CGREctMake(0,0,0,0)]; } and i init them all in my viewDidLoad @tdelepine – darkman Oct 01 '13 at 13:48
  • OK How to alloc init the textField IVar ? and what is the crash message – tdelepine Oct 01 '13 at 13:49
  • 2
    I've spent the past day on this exact same issue -- any time a first responder is not visible (for example, scrolled behind the keyboard) and its is resigned, **crash**. My textfield is not a global variable, so the crash isn't related to that. iOS7 only. – memmons Oct 07 '13 at 23:29
  • @Answerbot, thanks for the tip, I've successfully reproduced the crash on my app. – Zhao Xiang Oct 23 '13 at 09:53
  • @ZhaoXiang Thanks for the answer below. That was the root cause of my crashing as well. Great to finally have a fix for this. – memmons Dec 01 '13 at 17:32
  • darkman: You should mark Zhao's answer as correct. – memmons Dec 01 '13 at 17:33

3 Answers3

9

I've successfully fixed a similar crash bug with the help of Apple. The key is the reuseIdentifer.

The quote is from a mail from Vincent Gable of Apple Developer Technical Support:

This is a known behavior change that happens in iOS 7 with UITableView, when cells are not reused.

The fix here is to make sure that you follow proper cell reuse. If you do not want to re-use UITableViewCells, then it is recommended that you simply layout all your views inside a UIScrollView.

To make sure cells are re-used, make sure you are passing the same string to dequeueReusableCellWithIdentifier: that you pass to reuseIdentifier: when using alloc/init to make the cell. This string can not be nil.

So I think you should make sure you've set TableCell's reuseIdentifer property with the same value you've passed to dequeueReusableCellWithIdentifier:

Community
  • 1
  • 1
Zhao Xiang
  • 1,625
  • 2
  • 23
  • 40
  • +1 This fix works. In my case there was no reuseIdentifier set on the cell in InterfaceBuilder. This likely is not an issue if you are using the more modern approach to cell reuse via registering cells with the tableView, but, if you are still loading nibs manually via loadNibNamed -- as the legacy code I'm working with did -- it will almost certainly bite you at some point. – memmons Dec 01 '13 at 17:23
  • @MichaelG.Emmons, in my case I did not set the `reuseIdentifer` in IB neither. I've never noticed there's such property exist. – Zhao Xiang Dec 02 '13 at 10:03
1

You need to do some more research into how UITableViews work and reconsider your design. Storing a UITextField in a global variable and trying to position it like this is not the right approach. Even if you could solve the immediate problem, which is likely that the UITextField has been released along with the UITableViewCell, this design is only going to get you into trouble further down the line.

Instead, consider subclassing UITableViewCell and adding a UITextField property to your subclass.

You probably don't want to be using a different CellIdentifier for every single row either.

lintmachine
  • 245
  • 1
  • 11
0

Maybe i've solved. It's a little bit dirty methot but i think it work. I store all the cell that cellForRowAtIndexPath create

if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"FormCell_%@",cellID] owner:nil options:nil] lastObject]; [self.allTheCell addObject:cell]; } the app doesn't crash anymore on ios7

DigitalBrain_DEV
  • 1,093
  • 13
  • 27