UITableViewCell 中的 UITextField - 重用问题

UITextField in UITableViewCell - reuse issue

我有一个 UITableView,它有一个自定义的 UITableViewCell,里面有一个 UITextField

每个 UITextField 显示我的 viewModel 中的一些文本,我正在使用 Reactive-Cocoa 将文本字段绑定到 viewModel

当我的 UITableView 第一次加载时,一切正常。但是,当我为下一个 'page' 重新加载 UiTableView - 重新加载(第二页)tableView 上的第一个 UiTextField 与第一个 UITextField 中的第一个 UITextField 具有完全相同的内存地址'page' - 单元格与其他单元格不同 UI 元素是正确的 - 只是文本字段是相同的实例。

所以,我在我的 VC 中声明了 UITextFields:

@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 声明如下:

@property (strong, nonatomic) IBOutlet UITextField *textField;

我也尝试在单元的实现文件中执行此操作:

-(void)prepareForReuse
{
    self.textField = [[UITextField alloc] init];
}

我认为我的问题可能是某种细胞再利用问题。 但是这段代码没有任何区别。

所以 textFieldOnetextFieldTwo 都有完全相同的内存地址,我不明白为什么。

cellForRowAtIndexPath 中,我这样创建单元格:

BBTextFieldLabelTableViewCell *textFieldCell = [tableView dequeueReusableCellWithIdentifier:textFieldCellidentifier];

在您的 prepareForReuse 中,您正在创建一个新的文本字段,但您既没有删除旧文本字段也没有添加新文本字段。

我建议使用 prepareForReuse 来重置当前文本字段而不是创建一个新文本字段

仔细阅读您的问题:文本字段一和文本字段二具有相同的值这一事实表明,在两次调用 configureTextFieldCell 之间未调用准备重用。没有更多的代码,很难理解为什么