Editing a UITextField inside a UITableViewCell fails

前端 未结 7 1748
时光取名叫无心
时光取名叫无心 2020-12-04 11:24

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

相关标签:
7条回答
  • 2020-12-04 11:56

    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 讨论(0)
  • 2020-12-04 12:01

    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.

    0 讨论(0)
  • 2020-12-04 12:05

    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/

    0 讨论(0)
  • 2020-12-04 12:05

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

    0 讨论(0)
  • 2020-12-04 12:16

    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.

    0 讨论(0)
  • 2020-12-04 12:22

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

    0 讨论(0)
提交回复
热议问题