Avinash Vaghasiya Avinash Vaghasiya - 4 months ago 18
iOS Question

how to set UITableView's scroll position to previous Position in ios

When Click on textfield in cell, then open key pad and scroll tableview.

textFieldShouldReturn:
call then tableview set Previous Position. How can I do this?

My Tableview Scroll Code

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
CGPoint point = [textField.superview convertPoint:textField.frame.origin toView:tblemailconfiguration];
CGPoint contentOffset = tblemailconfiguration.contentOffset;
contentOffset.y += (point.y - contentOffset.y - self.navigationController.navigationBar.frame.size.height); // Adjust this value as you need
[tblemailconfiguration setContentOffset:contentOffset animated:YES];

return YES;
}

Answer

Just do like below:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    CGPoint point = [textField.superview convertPoint:textField.frame.origin toView:tblemailconfiguration];
    CGPoint contentOffset = tblemailconfiguration.contentOffset;
    // Record the tableview's content offset when editing begin
    lastContentOffset = contentOffset;
    contentOffset.y += (point.y - contentOffset.y - self.navigationController.navigationBar.frame.size.height); // Adjust this value as you need
    [tblemailconfiguration setContentOffset:contentOffset animated:YES];

    return YES;
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    // When editing reach end, just set tableview's content offset to last. 
    [tblemailconfiguration setContentOffset:lastContentOffset animated:YES];
}