Always show from the first index of the table?

2019-07-02 00:09发布

问题:

I have a UITableView in one of my view. Lets say the tableView has 100 index. If I scroll down and select 50th index, its pushing to another view.

So what I want is, if I pop back to tableView view from second view, the tableView should show from the first index.But currently its was in the index where I selected previously.Please refer the below image for better understanding.

Here "smart" is the 25th index.If I select smart, it pushes to another view(Detailed view).So if I again come back to this tableView, the table should reset and display from first view.How can I achieve this? I tried reloading table and it doesn't work.

Please suggest your ideas.

Thank you!!!

回答1:

Try something like this:

- (void)viewWillAppear:(BOOL)animated
{
    [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
}


回答2:

Use this for show first index in tableview.

 NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
    [self.tbl_presentation selectRowAtIndexPath:myIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

i hope this code useful for you.



回答3:

You need to ask the table view to scroll to the first row using scrollToRowAtIndexPath:atScrollPosition:animated: using a scroll position of UITableViewScrollPositionTop. You may want to do this after a delay depending on what other animations you have going on.



回答4:

Do anything in viewWillAppear method as it will be called when u come back.

- (void)viewWillAppear:(BOOL)animated
{
    [yourTableView.scrollView setContentOffset:CGPointMake(0,0)];
}


回答5:

Save the row index or value before navigating to another view which you want to keep highlighted and show it on view.

After that when you come back to the same view then just add the following line:

// rowIndex is saved row value before navigating to another view.
[tableView selectRowAtIndexPath:rowIndex
                       animated:NO 
                 scrollPosition:UITableViewScrollPositionMiddle];

This will solve your problem.