简体   繁体   中英

Why does a UITextField as a subview of a custom UITableViewCell inhibit the scrolling of the parent UITableView?

I have created EDITableViewCell , a subclass of UITableViewCell , so that I can put a UITextField inside of it, rather than a UILabel . I use EDITableView when overriding tableView:cellForRowAtIndexPath: . I populate a few cells with dummy data when initializing the app. The app seems to run fine. The UITableView is displayed as expected with three rows of the table populated with dummy data. The problem I am experiencing has do with scrolling the table.

  • If I pull up or down on the middle of any of the populated rows, there is no scrolling in response.
  • If I pull up or down on the middle of any of the unpopulated rows (something below the third row), there is scrolling in response, as would be expected with any UITableView . Because there are only three items, the table view bounces right back to where it was on touch up.
  • If I pull up or down on the edges of any of the populated rows (the portions of the cell that the UITextField doesn't extend to), there is scrolling in response as well.

Note: I have experienced this same (mis)behavior both with a programmatically-created subclassed UITableViewCell approach as well as with a prototype Storyboard approach.

It seems to me that the UITextField is in some way inhibiting the scrolling of my UITableView . Why would this be happening?

If this is inevitable, how would I go about creating a UITableViewCell subclass that does allow for in-place editing without inhibiting the scrolling?

Motivation: Like the edit event portion of Apple's Calendar app and many ToDo list apps, I want a UITableView full of cells that can be edited in-place, as opposed to segueing or using modals to edit elsewhere.

The issue here is having a UIResponder object (your UITextField) that is capturing the tap event and basically holding it so the UIScrollView (your table view) does not receive those events. A couple of suggestions for you to try:

  • Create a UITextField subclass that forwards touch events to the next responder.
  • Disable user interaction on the text field, and program when you want it to respond by calling becomeFirstResponder when needed.

For the first suggestion, your subclass would do something like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesBegan:touches withEvent:event];
}

But of course you would need to forward the other UIResponder methods as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM