How to stop UITableView from returning to top cell after popover

后端 未结 4 1328
春和景丽
春和景丽 2021-01-24 13:47

I have a tableView full of images. When a user taps on an image (within the cell), a viewController with a larger zoomable version of the image is called with a popover segue. W

相关标签:
4条回答
  • 2021-01-24 14:01

    please check, if you are calling reloadData in viewWillAppear or viewDidAppear. or if you are calling any reloadsection in either of these 2 methods.

    You can also validate these calls also in viewWillDisappear/viewDidDisappear methods too.

    0 讨论(0)
  • 2021-01-24 14:13

    Cache selected indexPath of the selected image cell, and in viewWillAppear method,

        if(selectedPath != nil) {
            [self.tableView scrollToRowAtIndexPath:selectedPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
         }
    

    This will scroll back to your selected image row.

    0 讨论(0)
  • 2021-01-24 14:14

    most probably the reason for this behavior is that you are calling table reload some where between opening and closing the popover view (probably inside one or more of these methods like viewDidAppear, viewWillAppear etc). If you can simply remove it then it wont be scrolling to top.

    or you can keep the content offset just before opening popover view then set the offset after closing it,

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        tableView.contentOffset = offset
    }
    
    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        offset = tableView.contentOffset
    }
    
    0 讨论(0)
  • 2021-01-24 14:17

    Get tableview content offset before displaying your zoomViewController.

    -(void)viewWillDisappear:(BOOL)animated {
    
      [super viewWillDisappear:animated];
    
      tableViewContentOffset = self.tableView.contentOffset;
    
    }
    

    and set tableview offset again in viewWillAppear

    -(void)viewWillAppear:(BOOL)animated {
    
       [super viewWillAppear:animated];
    
       [self.tableView setContentOffset:tableViewContentOffset];
     }
    
    0 讨论(0)
提交回复
热议问题