In my application I have a UITextField
inside a UITableViewCell
. If I click inside the text field and add some text I find that if try to move the insertion point it works the first time but fails on subsequent attempts. I am completely unable to move the selection; no "magnifying glass" appears.
Even more curious, this "setting" seems to be permanent until I restart the application. And it affects all UITextFields
on that screen and not just the one that I originally tried to edit.
If you want to see it yourself, try the "UICatalog" sample that comes with the iPhone SDK. Click "text fields" and then "edit" and play around with the text boxes.
I've done a lot of digging on this but it's pretty hard to Google for! The best references I've found are on Apple's support board and MacRumors formum (both reference a solution that apparently used to work on iPhone 2.0 but does work not with contemporary versions -- I did try).
My feeling that is that this is a bug in the OS, but I thought I'd throw this out to the SO crowd for a second opinion and to see if there are any workarounds. Any ideas?
Following benzado's suggestion, I tried building my application using the 2.0, 2.1 and 2.2 SDKs. I got the same behaviour in all versions. (Actually, something related but not the same broke in 2.2 but that's probably another question!)
I spent a lot of time on this but I finally think that I have it nailed.
The trick is that the table needs to be editable (i.e., its editing
property needs to be set to YES
). The good news is that you are now able to move the insertion point. Sometimes the magnifying glass doesn't appear or follow but your gesture always seems to work.
Does this still qualify as a bug? Perhaps. At the very least Apple's SDK documentation should be updated. I've raised a bug report with Apple to cover this (rdar://6462725).
Thanks to this post, I've been able to successfully get this to work properly in my app.
Something to add, however:
If you set your table to be editable, you'll likely get different behavior than you expect (indenting, editing widgets, no disclosure indicators, etc.). This surprised me but here's how to best deal with it:
In your UITableView delegate, implement:
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
Then, in your UITableViewCell's implementation, set your UITableView to be editable ONLY when you're actually editing:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
((UITableView *)[self superview]).editing = YES;
...
}
and disable editing when editing is done:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
...
((UITableView *)[self superview]).editing = YES;
}
This will ensure that your table isn't in editing mode when you're not editing the cell, keeping things working smoothly.
Thanks!
Brian M. Criscuolo Mark/Space Inc.
The answer by Brian M. Criscuolo is the closest, however its still not quite right - in my usage of SDK2.2.1 I find that I have to do the following:
To your UITableViewDelegate (which is often your UITableViewController) add both of the following:
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
and:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
and:
- (void)viewDidLoad {
[super viewDidLoad];
// do any other customisation here
self.uiTableView.editing = true;
}
If you don't put the top two delegate methods, the above will cause the delete icons next to each row, and the indentation of each row.
You shouldn't need to do anything with textfield delegates as Brian indicated (unless you have multiple rows and you want to respond to a didSelectRowAtIndexPath: event - which you don't seem to get while in edit mode - then you will need to also do as he suggests).
By the way - this seems fixed in SDK3.0 (although subject to change I guess)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With