SRMR
SRMR

Reputation: 3134

Start TableView at specific index path row

I have a a tableView that I want to show on screen at a specific indexPath.row.

For example, say each tableViewCell takes up half of the screen, and I want the screen to appear at indexPath.row of 6, instead of always starting at indexPath.row of 0.

Is there any way to do this?

To make clearer, I want to have the table view appear scrolled such that rows 0-5 are scrolled off-screen, and row 6 is the top row in the table view.

Upvotes: 0

Views: 986

Answers (2)

Joel Nieman
Joel Nieman

Reputation: 746

In Swift you can use something like this:

func scrollToSelectedPosition() {
    let indexPath = NSIndexPath(forRow: selectedPositionInt!, inSection: 0)
    self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: false)
}

Where selectedPositionInt is the position you want to scroll to.

Upvotes: 1

Scrungepipes
Scrungepipes

Reputation: 37581

You can use scrollToRowAtIndexPath:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:N inSection:M];
    [yourTableView scrollToRowAtIndexPath:indexPath 
                         atScrollPosition:UITableViewScrollPositionTop 
                                 animated:YES];

You can experiment with these positions until you are happy with the results

UITableViewScrollPositionNone
UITableViewScrollPositionTop
UITableViewScrollPositionMiddle
UITableViewScrollPositionBottom

Upvotes: 3

Related Questions