Disabling automatic scrolling of UITableView when editing UITextField inside UITableViewCell

后端 未结 9 910
别跟我提以往
别跟我提以往 2020-11-30 00:40

I\'m using custom UITableViewCells inside my UITableView. Each of these UITableViewCells is pretty high and contains a UITextFie

9条回答
  •  难免孤独
    2020-11-30 01:10

    Define properties for your UITableViewController:

    @property (nonatomic) BOOL scrollDisabled;
    @property (nonatomic) CGFloat lastContentOffsetY;
    

    Before you call becomeFirstResponder:

    // Save the table view's y content offset 
    lastContentOffsetY = tableViewController.tableView.contentOffset.y;
    // Enable scrollDisabled
    scrollDisabled = YES;
    

    Add the following code to your table view controller:

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        if (self.scrollDisabled) {
            [self.tableView setContentOffset:CGPointMake(0, lastContentOffsetY)];
        }
        ...
    }
    

    After you call resignFirstResponder, set scrollDisabled = NO.

提交回复
热议问题