UITextField in UITableViewCell - reuse issue

落爺英雄遲暮 提交于 2019-12-12 00:13:34

问题


I have a UITableView which has a custom UITableViewCell that has a UITextField inside it.

Each UITextField displays some text from my viewModel and I am using Reactive-Cocoa to bind the textfields to the viewModel.

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.

So, I declared the UITextFields in my VC like so:

@property (weak, nonatomic)  UITextField *textFieldOne; //One the first 'page'
@property (weak, nonatomic)  UITextField *textFieldTwo; //After reload on second 'page' 

Then setup then up like so in a method invoked by 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 */
        }
    }

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.

So textFieldOne and textFieldTwo both have the exact same memory address and I cannot figure out why.

Inside cellForRowAtIndexPath I create the cell like so:

BBTextFieldLabelTableViewCell *textFieldCell = [tableView dequeueReusableCellWithIdentifier:textFieldCellidentifier];

回答1:


In your prepareForReuse you are creating a new text-field but you are neither removing the old one nor adding the new one.

I suggest using prepareForReuse to reset the current text field rather than creating a new one

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



来源:https://stackoverflow.com/questions/31532231/uitextfield-in-uitableviewcell-reuse-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!