UITableViewCell 中的 UITextField - 重用问题 [英] UITextField in UITableViewCell - reuse issue
问题描述
我有一个 UITableView
,它有一个自定义的 UITableViewCell
,里面有一个 UITextField
.
I have a UITableView
which has a custom UITableViewCell
that has a UITextField
inside it.
每个 UITextField
显示来自我的 viewModel
的一些文本,我使用 Reactive-Cocoa
将文本字段绑定到 viewModel代码>.
Each UITextField
displays some text from my viewModel
and I am using Reactive-Cocoa
to bind the textfields to the viewModel
.
当我的 UITableView
首次加载时,一切正常.但是,当我为下一个页面"重新加载 UiTableView
时 - 重新加载(第二页)tableView 上的第一个 UiTextField
与第一个 UITextField 具有完全相同的内存地址
在第一个页面"中 - 单元格与其他 UI 元素不一样是正确的 - 只是文本字段是相同的实例.
When my UITableView
loads for the first time, everything works just fine. However when I reload the UiTableView
for the next 'page' - the first UiTextField
on reloaded (page two) tableView has the exact same memory address as the first UITextField
in the first 'page' - cell is not the same as other UI Elements are correct - just the textfields are the same instance.
所以,我在 VC 中声明了 UITextField
,如下所示:
So, I declared the UITextField
s in my VC like so:
@property (weak, nonatomic) UITextField *textFieldOne; //One the first 'page'
@property (weak, nonatomic) UITextField *textFieldTwo; //After reload on second 'page'
然后在 cellForRowAtIndexPath
-(void)configureTextFieldCell:(BBTextFieldLabelTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
cell.textField.delegate = self;
if (self.selectedSegmentIndex == SegmentedControlStep1){
if (indexPath.section == 0){
cell.label.text = @"Name";
self.textFieldOne = cell.textField;
}
/* Code for setting up other cells / textfields ommited -
but the same syntax as above with checks for indexPath */
}
if (self.selectedSegmentIndex == SegmentedControlStep2){
cell.label.text = @"Username";
self.textFieldTwo = cell.textField;
[self bindUsernameAndPasswordToViewModel]; /* Binding for this textfield as its nil when VC loads for the first time,
so this is the first chance I get to bind it on second page */
}
}
在 BBTextFieldLabelTableViewCell
中,UITextField
声明如下:
Inside BBTextFieldLabelTableViewCell
the UITextField
is declared like so:
@property (strong, nonatomic) IBOutlet UITextField *textField;
我也尝试在单元的实现文件中这样做:
I also tried doing this inside the cell's implementation file:
-(void)prepareForReuse
{
self.textField = [[UITextField alloc] init];
}
我认为我的问题可能是某种细胞重用问题.但是,此代码没有任何区别.
As I thought my issue is possibly a cell-reuse issue of some sort. However this code made no difference.
所以 textFieldOne
和 textFieldTwo
都具有完全相同的内存地址,我不知道为什么.
So textFieldOne
and textFieldTwo
both have the exact same memory address and I cannot figure out why.
在 cellForRowAtIndexPath
中,我像这样创建单元格:
Inside cellForRowAtIndexPath
I create the cell like so:
BBTextFieldLabelTableViewCell *textFieldCell = [tableView dequeueReusableCellWithIdentifier:textFieldCellidentifier];
推荐答案
在您的 prepareForReuse 中,您正在创建一个新的文本字段,但您既没有删除旧的,也没有添加新的.
In your prepareForReuse you are creating a new text-field but you are neither removing the old one nor adding the new one.
我建议使用 prepareForReuse 重置当前文本字段而不是创建一个新的文本字段
I suggest using prepareForReuse to reset the current text field rather than creating a new one
更仔细地阅读您的问题:文本字段一和二具有相同值这一事实表明,在两次调用 configureTextFieldCell 之间没有调用准备重用.没有更多的代码,很难理解为什么
Reading your question a bit more carefully: The fact that textfield one and two both have the same value, indicates that prepare for reuse is not being called between the two calls to configureTextFieldCell. Without more code, it is difficult to understand why
这篇关于UITableViewCell 中的 UITextField - 重用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!