UITableView - scroll to the top

前端 未结 30 2734
南方客
南方客 2020-11-28 17:22

In my table view I have to scroll to the top. But I cannot guarantee that the first object is going to be section 0, row 0. May be that my table view will start from section

相关标签:
30条回答
  • 2020-11-28 17:51

    I had to add the multiply by -1 * to the sum of the status bar and the navigation bar, because it was going that height off the screen,

    self.tableView.setContentOffset(CGPointMake(0 , -1 * 
      (self.navigationController!.navigationBar.height +  
      UIApplication.sharedApplication().statusBarFrame.height) ), animated:true)
    
    0 讨论(0)
  • 2020-11-28 17:52

    It's better to not use NSIndexPath (empty table), nor assume that top point is CGPointZero (content insets), that's what I use -

    [tableView setContentOffset:CGPointMake(0.0f, -tableView.contentInset.top) animated:YES];
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-28 17:52

    I use tabBarController and i have a few section in my tableview at every tab, so this is best solution for me.

    extension UITableView {
    
        func scrollToTop(){
    
            for index in 0...numberOfSections - 1 {
                if numberOfSections > 0 && numberOfRows(inSection: index) > 0 {
                    scrollToRow(at: IndexPath(row: 0, section: index), at: .top, animated: true)
                    break
                }
    
                if index == numberOfSections - 1 {
                    setContentOffset(.zero, animated: true)
                    break
                }
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 17:54

    I've encountered an issue calling trying some of the methods on an empty tableView. Here's another option for Swift 4 that handles empty tableviews.

    extension UITableView {
      func hasRowAtIndexPath(indexPath: IndexPath) -> Bool {
        return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRows(inSection: indexPath.section)
      }
    
      func scrollToTop(animated: Bool) {
        let indexPath = IndexPath(row: 0, section: 0)
        if self.hasRowAtIndexPath(indexPath: indexPath) {
          self.scrollToRow(at: indexPath, at: .top, animated: animated)
        }
      }
    }
    

    Usage:

    // from yourViewController or yourTableViewController
    tableView.scrollToTop(animated: true)//or false
    
    0 讨论(0)
  • 2020-11-28 17:54

    using contentOffset is not the right way. this would be better as it is table view's natural way

    tableView.scrollToRow(at: NSIndexPath.init(row: 0, section: 0) as IndexPath, at: .top, animated: true)
    
    0 讨论(0)
  • 2020-11-28 17:55

    Note: This answer isn't valid for iOS 11 and later.

    I prefer

    [mainTableView setContentOffset:CGPointZero animated:YES];
    

    If you have a top inset on your table view, you have to subtract it:

    [mainTableView setContentOffset:CGPointMake(0.0f, -mainTableView.contentInset.top) animated:YES];
    
    0 讨论(0)
提交回复
热议问题