0

I have a UITableView that has something like 30 cells in it. I successfully use the scroll to index method when I add a new cell, but it is not working (almost) when called at view did load. When it is called at view didload it scrolls to the second to last cell instead of the last one, leaving the last one still hidden beyond view. There is definitely an object in the last position of the array at the point i am calling the scroll to, it is just weird.

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[myMatch.chat count]-1 inSection:0];
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
rishi
  • 11,779
  • 4
  • 40
  • 59
michaela
  • 173
  • 1
  • 2
  • 13

3 Answers3

3

Use -

[tableView setContentInset:UIEdgeInsetsMake(0, 0, bottomSpace, 0)];

UIEdgeInsetsMake takes four parameters - top, left, bottom, right.

rishi
  • 11,779
  • 4
  • 40
  • 59
  • i do that but it does not work, maybe it is not updating? how do i update it? – michaela May 20 '12 at 12:22
  • where you have added this code? Add this code where you have initialised the table view, and for testing purpose initially you can pass value like 300 and then test if this works fine for you? – rishi May 20 '12 at 12:25
  • Not working... I have read that the method I questioned about should work, but why is it not? – michaela May 20 '12 at 12:36
0

Michaela, I would think in 3 possible ways:

  • NSLog the index number you are calling when you call the scroll method. Try firing this method also from a IBAction and see if the logged value and results is different.
  • Are you sure the UITableView frame is set correctly?
  • Did you try different UITableViewScrollPosition?
Natan R.
  • 5,141
  • 1
  • 31
  • 48
  • what do you mean correctly? i set it up using ib. the only time it doesn't scroll to the right place is on viewdidload – michaela May 20 '12 at 14:20
  • I edited the question, added one more possibility. I thought maybe some part of the TableView could be hidden, for being out of the view. Try, in the same viewDidLoad method do make some NSLog(@"Row %d", [myMatch.chat count]-1); And check also if the method works if fired from some IBAction/button – Natan R. May 20 '12 at 14:23
0

Does the functionality work correctly when called from viewDidAppear:? If so, it might have something to do with the view resizing after viewDidLoad is called.

See: Why am I having to manually set my view's frame in viewDidLoad?

Are you perhaps having the view set up in interface builder for the iPhone 5 and seeing these results on an iPhone 4? My guess would be that the bounds (the actual part of the tableview being shown) is set in viewDidLoad and then the frame is modified from its original size by either Springs and Struts or Autolayout. The bounds always get pushed up from the bottom, I believe, so that might be what covers up your last cell.

You can test this by NSLog(NSStringFromCGRect(self.view.frame)); in both the viewDidAppear and viewDidLoad. If they are different, the frame is resizing and covering up your last item after you have already scrolled to the bottom.

Community
  • 1
  • 1
btomw
  • 2,512
  • 2
  • 20
  • 25