Always show from the first index of the table?

2019-07-02 00:22发布

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.

enter image description here

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!!!

5条回答
我命由我不由天
2楼-- · 2019-07-02 00:44

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.

查看更多
我只想做你的唯一
3楼-- · 2019-07-02 00:50

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

- (void)viewWillAppear:(BOOL)animated
{
    [yourTableView.scrollView setContentOffset:CGPointMake(0,0)];
}
查看更多
We Are One
4楼-- · 2019-07-02 00:56

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.

查看更多
劳资没心,怎么记你
5楼-- · 2019-07-02 01:01

Try something like this:

- (void)viewWillAppear:(BOOL)animated
{
    [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
}
查看更多
男人必须洒脱
6楼-- · 2019-07-02 01:06

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.

查看更多
登录 后发表回答