简体   繁体   English

EXC_BAD_ACCESS重装带有文本字段的单元格

[英]EXC_BAD_ACCESS reloading cell with textfield inside

I have a problem in my app and can t find a solution. 我的应用程序有问题,无法找到解决方案。

I have a UICollectionView with custom cells. 我有一个带有自定义单元格的UICollectionView My cells have a UITextField inside. 我的单元格内部有一个UITextField。

When textfield is in editMode (with the keyboard active), if i reload the collectionView i have a EXC_BAD_ACCESS error. 当textfield处于editMode(激活键盘)时,如果我重新加载collectionView,则会出现EXC_BAD_ACCESS错误。 This error happen because the call of the method resignFirstResponder of the UITextField . 发生此错误是因为UITextField的方法resignFirstResponder的调用。 The problem is the cell probably don´t exist anymore inside the collectionView, since it was dealloced. 问题在于该单元可能已不存在,因为它已被取消分配。

This is my stack on the error: 这是我对错误的堆栈:

在此处输入图片说明

Any idea how to avoid this problem? 任何想法如何避免这个问题?

Thanks in advance 提前致谢

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM