Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting scroll position in UITableView

I have an application that works some what similar to how iPhone's Contact application works. When we add a new Contact user is directed to a view only screen with Contact information. If we select "All Contacts" from the navigation bar, user is navigated to list of all contacts where the recently added contact is in view.

We can move the view to a particular row using:

    [itemsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];

... but it's not working. I'm calling this right after calling:

    [tableView reloadData];

I think I'm not suppose to call selectRowAtIndexPath:animated:scrollPosition method here. But if not here, then where?

Is there any delegate method that gets called after the following method?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
like image 775
Mustafa Avatar asked Jan 05 '09 13:01

Mustafa


3 Answers

I think I've got a similar app: A list of item., tap '+' -> new screen, come back and see the updated list, scrolled to show the added item at the bottom.

In summary, I put reloadData in viewWillAppear:animated: and scrollToRowAtIndexPath:... in viewDidAppear:animated:.

// Note: Member variables dataHasChanged and scrollToLast have been
// set to YES somewhere else, e.g. when tapping 'Save' in the new-item view.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (dataHasChanged) {
        self.dataHasChanged = NO;
        [[self tableView] reloadData];
    } else {
        self.scrollToLast = NO; // No reload -> no need to scroll!
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (scrollToLast) {
        NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([dataController count] - 1) inSection:0];
        [[self tableView] scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
}

I hope this helps. If you add something in the middle of the list, you could easily scroll to that position instead.

like image 91
squelart Avatar answered Sep 20 '22 14:09

squelart


You can try this , my application in some kind of similar to you when i click on a button scroll of uitableview is up.

[tableviewname setContentOffset:CGPointMake(0, ([arrayofcontact count]-10)*cellheight) animated:YES];

10 is for how many cell you want to scroll up

Hope this will help you:-)

like image 30
Birju Avatar answered Sep 20 '22 14:09

Birju


Scrolling animation is inevitable when offset is set from viewDidAppear(_:). A better place for setting initial scroll offset is viewWillAppear(_:). You'll need to force the layout of a table view, because it's content size is not defined at that point.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    tableView.setNeedsLayout()
    tableView.layoutIfNeeded()

    if let selectedIndexPath = selectedIndexPath, tableView.numberOfRows(inSection: selectedIndexPath.section) > 0 {
        tableView.scrollToRow(at: selectedIndexPath, at: .top, animated: false)
    }
}
like image 27
Vadim Bulavin Avatar answered Sep 20 '22 14:09

Vadim Bulavin