Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS 7 UITextField resignFirstResponder BAD

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;
}
like image 359
darkman Avatar asked Oct 01 '13 13:10

darkman


2 Answers

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:

like image 120
Zhao Xiang Avatar answered Sep 19 '22 07:09

Zhao Xiang


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.

like image 38
lintmachine Avatar answered Sep 21 '22 07:09

lintmachine