开发者

How do I prevent my table cell's textview from being editable after editing is done?

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!


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜