简体   繁体   中英

UITextField in UITableViewCell - reuse issue

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 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' 

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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