How to get a UITableview to go to the top of page on reload?

前端 未结 13 1914
一个人的身影
一个人的身影 2021-02-01 02:30

I am trying to get a UITableview to go to the top of the page when I reload the table data when I call the following from

- (void)pickerView:(UIPickerView *)pic         


        
相关标签:
13条回答
  • 2021-02-01 02:39

    After reading all of the answers here and elsewhere, I finally found a solution that works reliably and does not show the scrolling after the data is loaded. My answer is based on this post and my observation that you need a negative offset if you are using content insets:

    func reloadAndScrollToTop() {
        tableView.reloadData()
        tableView.layoutIfNeeded()
        tableView.contentOffset = CGPoint(x: 0, y: -tableView.contentInset.top)
    }
    
    0 讨论(0)
  • 2021-02-01 02:42

    In swift 3.1

    DispatchQueue.main.async(execute: {
    
            self.TableView.reloadData()
            self.TableView.contentOffset = .zero
    
        })
    
    0 讨论(0)
  • 2021-02-01 02:44

    Perfectly to the top of page on reload, by twice reloading

    dataForSource = nil
    tableView.reloadData()
    
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
        self.dataForSource = realData
        self.tableView.reloadData()
    }
    
    0 讨论(0)
  • 2021-02-01 02:45

    I was a bit confused by some of the answers I found to this question due to incomplete explanations. From what I've gathered, there's two routes to accomplish what OP requested and they are as follows:

    1. If animation isn't a requirement, I'd suggest reseting scroll position before data loads using one of the offset methods. The reason you should run offset before reloadData is that reloadData runs asynchronously. If you run offset after reload, your scroll position will probably be a few rows down from the top.

      tableView.setContentOffset(.zero, animated: false)
      tableView.reloadData()
      
    2. If animation is a requirement, position your scroll to the top row after you run reloadData using scrollToRow. The reason scrollToRow works here is that it positions you at the top of the table, versus the offset method that positions you relative to data that has already loaded.

      tableView.reloadData()
      tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
      
    0 讨论(0)
  • 2021-02-01 02:48

    Here's another way you could do it:

    Objective-C

    [tableView setContentOffset:CGPointZero animated:YES];
    

    Swift 3 and higher

    tableView.setContentOffset(.zero, animated: true)
    

    Probably the easiest and most straight forward way to do it.

    0 讨论(0)
  • 2021-02-01 02:48

    WITHOUT ANIMATION

    I'm on iOS11, using a plain tableview style with sticky headers and somehow the only way I got this to work correctly to have the tableview really on top after a reload without any strange flickers/animation behaviours was to use these methods in this order where ALL of these are necessary:

    [self.tableView setContentOffset:CGPointZero animated:NO];
    [self.tableView reloadData];
    [self.tableView layoutIfNeeded];
    [self.tableView setContentOffset:CGPointZero animated:NO];
    

    I'm serious that all of these are necessary, even though it seems the first line is not relevant at all. Oh yeah, also very important, do NOT use this:

    self.tableView.contentOffset = CGPointZero;
    

    You would think that it's the same as the "setContentOffset animated:FALSE" but apparently it's not! Apple treats this method differently and in my case this only worked when using the full method with animated:FALSE.

    WITH ANIMATION

    I also wanted to try this with a nice scrolling animation to the top. There the content offset methods seemed to still cause some strange animation behaviours. The only way I got this working with a nice animation after some trial and error were these methods in this exact order:

    [self.tableView reloadData];
    [self.tableView layoutIfNeeded];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:TRUE];
    

    Warning, make sure you have at least 1 row before running the last line of code to avoid crashing :)

    I hope this helps someone someday!

    0 讨论(0)
提交回复
热议问题