views:

184

answers:

3

So this is an interesting problem. I have custom tableviewcells that include a text field. When In my cellForRowAtIndexPath I have an if statement that determines whether or not the cell's text field should be editable- it looks like this:

(self.isEditing) ? [infoCell.textField setEnabled:YES] : [infoCell.textField setEnabled:NO];

This actually works well - except for the issue I'm having. It makes it so that when the tableview is displayed, the rows' text field cannot be edited. When the user clicks "Edit" to put it into editing mode, then the text fields are enabled for editing.

The Problem: When I am editing a field, and click "Done", it goes back to the regular tableview but the keyboard stays visible and the last cell's text field I was editing continues to be editable.

What Should happen: The keyboard should go away and all the cells' text fields should no longer be editable.

Any ideas about what could be going wrong? Things to look for?

Thanks!

+1  A: 

I've found self.isEditing to be unreliable. If you are editing an individual cell, it works differently from when you are in "edit mode".

What I've done to get around it is, whenever I want to do something to all other cells, I just iterate through my table view's visibleCells method and manually adjust them. You'll have to consider what happens when new cells become visible, but that's up to your implementation.

NSArray *visibleCells = [self.tableView visibleCells];
for (UITableViewCell *cell in visibleCells) {
    [cell doSomething];
}

PS - obviously you may want to skip the cell in question when iterating through the visible squares. depends on what you're doing.

DougW
Thanks for your suggestion. I played with it but I don't think it will work for this. The cells are custom cells and it's the text field inside the cells that I need to enable or disable, not the cell itself. Currently I'm doing this in the cellForRowAtIndexPath and it just won't work for the cell that's being edited when I switch from editing mode to regular mode... Very bizarre. And btw, you're right about self.isEditing being unreliable, i'm using my own isEditing variable.
Gorgando
+1  A: 

Unfortunately, disabling the UITextField won't dismiss the keyboard. You'll need to retain a pointer to your current UITextField. First, create an instance variable in your header file:

UITextField *currentTextField;

Then, implement the UITextFieldDelegate protocol. The dirty work will be done in the following method:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    currentTextField = textField;
}

Finally, when you're ready to dismiss the keyboard and disable your textFields, simply call:

[currentTextField resignFirstResponder];
[textField1 setEnabled:NO];
[textField2 setEnabled:NO]; //ad nauseum

Good luck!

Reed Olsen
That was perfect!!!!
Gorgando
A: 

Hi Gorgando,can u please help me to make a table cell editable?.I need to place a textfield inside a cell.Thanks in advance.

santosh kumar