EXC_BAD_ACCESS reloading cell with textfield inside

211 Views Asked by At

I have a problem in my app and can t find a solution.

I have a UICollectionView with custom cells. My cells have a UITextField inside.

When textfield is in editMode (with the keyboard active), if i reload the collectionView i have a EXC_BAD_ACCESS error. This error happen because the call of the method resignFirstResponder of the UITextField. The problem is the cell probably don´t exist anymore inside the collectionView, since it was dealloced.

This is my stack on the error:

enter image description here

Any idea how to avoid this problem?

Thanks in advance

1

There are 1 best solutions below

0
On BEST ANSWER

Add a reference in the model to the textfield, weak reference.

When the model will be dealloc, resign the responder of the textfield.

Code:

-(void)dealloc{
    if (self.refrenceTextField) {
        [self.refrenceTextField resignFirstResponder];
        self.refrenceTextField = nil;
    }
}