-1

I have UITableViewController want to scroll when user tap on the bottom of the textfield using scrollToRowAtIndexPath method

// ViewController

@interface PersonalInfoViewController : UITableViewController{
}

On the textFiled Delegate methods.

-(void) textFieldDidBeginEditing:(UITextField *)textField {
    UITableViewCell *textFieldRowCell = (UITableViewCell *) textField.superview.superview;;
    NSIndexPath* path = [self.tableView indexPathForCell:textFieldRowCell];
    [self performSelector:@selector(scrollToCell:) withObject:path afterDelay:0.5f];
}

// Scroll Method

-(void) scrollToCell:(NSIndexPath*) path {
    NSLog(@"%@",path);
    [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:NO];
}

Console O/P

// Size

NSLog(@"%@",NSStringFromCGSize(self.tableView.contentSize));  {320, 711.5}

// Bounds

NSLog(@"%@",NSStringFromCGRect(self.tableView.frame)); {{0, 0}, {320, 568}}

// IndexPath

<NSIndexPath: 0x8e981f0> {length = 2, path = 3 - 0}
<NSIndexPath: 0x8eaa600> {length = 2, path = 3 - 1}
<NSIndexPath: 0x9967d20> {length = 2, path = 2 - 1}

So question is that.

  • Why tableView not move to particular indexPath in scrollToRowAtIndexPath.
  • Where and how to use scrollToRowAtIndexPath.

I don't think so to use scrollToRowAtIndexPath we need to set contectSize and ContectOffset

Guys check my contect Size and Bounds o/p. How can you say that contect size is smaller than height

I found that UITableViewScrollPositionTop working for the top some of the row but not for the bottom of the last 4 to 5

user3772344
  • 153
  • 2
  • 10

3 Answers3

3

Here is the answer to your question: Because your content height is smaller than tableview bounds height.

here is working solution if you really need it

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    CGPoint scrollPoint = CGPointMake(0, textField.frame.origin.y);
    scrollPoint = [_tableView convertPoint:scrollPoint fromView:textfield.superview];
    [_tableView setContentOffset:scrollPoint animated:YES];
}

if you are curious, you can go deeper in the question here : How to make a UITextField move up when keyboard is present?

Community
  • 1
  • 1
kas-kad
  • 3,736
  • 1
  • 26
  • 45
  • [self.tableView setContentSize:CGSizeMake(320, 2000)]; i have set static still is not working – user3772344 Jul 06 '14 at 11:36
  • because tableview can not scroll breaking the bounce edge with `scrollToRowAtIndexPath` – kas-kad Jul 06 '14 at 11:38
  • please check my question – user3772344 Jul 06 '14 at 11:44
  • @user3772344 try `[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:NO];` and `[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionBottom animated:NO];` and let me know if it scrolls or not – kas-kad Jul 06 '14 at 11:47
  • I found that UITableViewScrollPositionTop working for the top some of the row but not for the bottom of the last 4 to 5 – user3772344 Jul 06 '14 at 11:56
  • @user3772344 It does not works for bottom cells, indeed. Again, because tableview can not scroll crossing the contents bounds. – kas-kad Jul 06 '14 at 12:12
  • Thanks! This worked to solve the problem in my case. I have no idea why. – Daniel Apr 07 '15 at 22:42
0

You need to make sure that tableView's contentSize is large enough to bring the cell to those positions. In these situations you need to calculate the contentOffset manually for those cells. Try below:-

-(void) textFieldDidBeginEditing:(UITextField *)textField {
 CGPoint origin = textField.frame.origin;
CGPoint point = [textField.superview convertPoint:origin toView:self.tableView];
float navBarHeight = self.navigationController.navigationBar.frame.size.height;
CGPoint offset = tableView.contentOffset;   
// Adjust the below value as you need
offset.y += (point.y - navBarHeight);
[tableView setContentOffset:o animated:YES];
}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • But my question is that why scrollToRowAtIndexPath not working. why its not scrolling to particular indexPath – user3772344 Jul 06 '14 at 11:28
  • Because your content height is smaller than tableview bounds height. – Hussain Shabbir Jul 06 '14 at 11:30
  • How contact height is smaller. can tableView automatically not set contact height. I have set static [self.tableView setContentSize:CGSizeMake(320, 2000)]; still is not working – user3772344 Jul 06 '14 at 11:34
0

I had a similar problem in one of my applications.

The general idea is to add a transparent view at the end of the UITableView. You can either add a footer or an additional cell. The height of the view should be: height of the tableView - height of the last visible cell.

Here is the solution I used:

- (void)updateFooterHeightForLastTableViewItem:(UITableView *)tableView {
    NSInteger sectionNumber = [tableView numberOfSections] - 1;
    CGFloat lastCellHeight = [self tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:[tableView numberOfRowsInSection:sectionNumber] inSection:sectionNumber]];
    _lastSectionFooterHeight = MAX(0, tableView.frame.size.height - lastCellHeight);
    [tableView reloadSections:[NSIndexSet indexSetWithIndex:sectionNumber] withRowAnimation:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section  {
    if (section == [tableView numberOfSections] - 1) {
        return _lastSectionFooterHeight;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    CGRect footerRect = CGRectMake(0, 0, 320, [self tableView:tableView heightForFooterInSection:section]);
    UIView *footerView = [[UIView alloc] initWithFrame:footerRect];
    [footerView setBackgroundColor:[UIColor clearColor]];
    [footerView setUserInteractionEnabled:NO];
    return footerView;
}
Wiecek
  • 96
  • 3