26

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

7 Answers 7

13

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

6
  • 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
    Dec 30, 2008 at 23:53
  • Thanks to both of you for this solution
    – Dan Morgan
    Jan 7, 2009 at 11:07
  • UITableView doesn't have an editable property, only UITextField does... are you talking about the /editing/ property on UITableView? or is it on UITableViewCell? Mar 18, 2009 at 16:51
  • Sorry, the description is correct but the property I mention is not. I mean the UITableView's "editing" property. I've updated my answer. Mar 19, 2009 at 10:16
  • 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. Mar 24, 2009 at 1:27
2

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.

2

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)

0

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/

1
  • 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... Dec 18, 2008 at 13:17
0

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.

1
  • Interesting. So how does your use of UITableView, UITableViewCell and UITextField differ from that in the UICatalog sample? Dec 20, 2008 at 18:30
0

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 NO;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

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

0

In iOS 12 or later you can select table view cell in the storyboard, then enable User Interaction Enabled in the attribute inspector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.