views:

10368

answers:

9

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!)

A: 

It does sound like an OS bug. I would try to reproduce it by running the UICatalog sample against the 2.0, 2.1, and 2.2 SDKs to see if anything changes. (There's a bug related to table cell text alignment that occurs if you build for 2.2 but not if you build for 2.1, regardless of what version of the OS is on the device.)

If it turns out to make a difference, https://bugreport.apple.com/

benzado
Thanks for the suggestions. I'll definitely try building with different versions of the SDK. I had assumed that it was the version of the OS that the code was running on, but of course I when you assume...
Stephen Darlington
A: 

I tried this with my application and it seems to work as expected. I get a magnifying glass every time. I am using the 2.1 SDK.

lostInTransit
Interesting. So how does your use of UITableView, UITableViewCell and UITextField differ from that in the UICatalog sample?
Stephen Darlington
+7  A: 

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 (6462725).

Stephen Darlington
Wow, I've just noticed the same issue in my app. I tried your trick and it now works fine, thanks!In addition, I had to make tableView:canEditRowAtIndexPath: and tableView:canMoveRowAtIndexPath: return NO, so that the table doesn't look like it's in edit mode!
squelart
Thanks to both of you for this solution
Dan Morgan
UITableView doesn't have an editable property, only UITextField does... are you talking about the /editing/ property on UITableView? or is it on UITableViewCell?
Andy Bourassa
Sorry, the description is correct but the property I mention is not. I mean the UITableView's "editing" property. I've updated my answer.
Stephen Darlington
You say "Sometimes the magnifying glass doesn't appear or follow but your gesture always seems to work" - in the simulator it seems that you *must* make some vertical movement for the magnifying glass to work, if you only move horizontally it won't show. Looks like an OS bug.
Michael Baltaks
I think so too. Apple flagged my bug report as a duplicate so hopefully they'll fix it at some point.
Stephen Darlington
A: 

Can you publish the solution: the code to build a UITableViewCell with a UITextField inside? Thank you very much!

Mathieu
My current implementation is just in Interface Builder -- simply drag and drop a text field onto a table cell.
Stephen Darlington
Oh, ok. I'm building my TableView dynamically, so doing that in IB is not going to work for me :(Thank you though!
Mathieu
+2  A: 

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.

Brian Criscuolo
A: 

hi , i have set the editing property to YES for table view. still the cursor not appears when i select the textField which is the subview of the table cell. Does we need to set any other property other than this ...

+2  A: 

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)

TimM
A: 

I'm seeing the same issue, in 3.1.2, but my UITextView was placed on a UIView in IB. It's not in anything else.

iPhoneDevDude
A: 

actually what works best seems to be to set the table "editing" property to true in "viewDidLoad" and adding these to the table delegate

  • (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath { return FALSE; }

  • (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return FALSE; }

you don't need to do anything in the text field delegate

billy bob