5

I have a UITableView which I would like to be displayed 100px down. For some reason this only works when animated is set to YES. Why is this?

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    /*[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] 
                          atScrollPosition:UITableViewScrollPositionNone 
                                  animated:NO];*/

    /*[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathWithIndex:1] 
                                        atScrollPosition:UITableViewScrollPositionNone 
                                        animated:NO];*/

    [self.tableView setContentOffset:CGPointMake(0,100) animated:YES];
}
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556

3 Answers3

0

Using the base Navigation-based Application that XCode supplies and giving it some table cells, it works fine with what you've given. Have you tried setting the content offset within viewDidAppear rather than viewWillAppear?

David Liu
  • 9,426
  • 5
  • 40
  • 63
0

What if you try to use setFrame instead of setContentOffset?

You should add this code in your viewDidAppear method:


[self.tableView setFrame:CGRectMake(0, 100, 320, 380)];

Depending on the other elements you have in your view (e.g. navigation controller, tool bar etc.) you will have to adjust the 380 to something else.

Alex
  • 7,432
  • 20
  • 75
  • 118
-1

Move the "setContentOffset" to "viewDidLoad" and add "reloadData" before that:

In Swift:

func viewDidLoad() {
    super.viewDidLoad()
    tableView.reloadData() // important for scrolling to be possible
    tableView.setContentOffset(newContentOffset), animated: false)
    }
hkdalex
  • 717
  • 7
  • 13