iOS UItableview scrollToRowAtIndexPath not working anymore

前端 未结 9 561
不知归路
不知归路 2020-12-30 01:14

This morning I just installed new Xcode which includes iOS 6.

I have a table view loaded with a plist file containing chapters and lines. Chapters define the section

相关标签:
9条回答
  • 2020-12-30 01:43

    One more possible workaround is to call layoutIfNeeded before calling scrollToRowAtIndexPath.

    [self.view layoutIfNeeded];
    [self.tableView scrollToRowAtIndexPath...];
    
    0 讨论(0)
  • 2020-12-30 01:44

    I'm adding this answer as an addition to Fyodor Volchyok's answer. I also found that dispatching solves the issue. I was able to find a workaround that doesn't dispatch.

    self.tableView.reloadData()
    let index = // the desired index path
    
    // For some reason, requesting the cell prior to 
    // scrolling was enough to workaround the issue.
    self.tableView.cellForRowAtIndexPath(index)
    
    self.tableView.scrollToRowAtIndexPath(index, atScrollPosition: .Top, animated: false)
    
    0 讨论(0)
  • 2020-12-30 01:44

    It worked for me in ios11

    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0
    
    dispatch_async(dispatch_get_main_queue(), ^{
       NSInteger numberOfSections = self.tableView.numberOfSections;
       if (numberOfSections > 0)
       {
         NSInteger lastSection = numberOfSections - 1;
         NSInteger lastRowInLastSections = [self.tableView numberOfRowsInSection:lastSection] - 1;
    
         NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastRowInLastSections inSection:lastSection];
         [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:isAnimated];
    }
    });
    
    0 讨论(0)
  • 2020-12-30 01:45

    This works in iOS 12 and 13:

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 
        self.tableView.scrollToRow(at: IndexPath(row: 0, section: 1), at: .bottom, animated: true) 
    }
    
    0 讨论(0)
  • 2020-12-30 01:52

    After iOS7 the property automaticallyAdjustsScrollViewInsets of UIViewController default is YES. It will cause system to adjust the contentOffset of tableView when the view controller pushed. Even you call [self.tableView scrollToRowAtIndexPath:rowIndexPath atScrollPosition:UITableViewScrollPositionNone animated:NO]; in the viewWillAppear. The contentOffset also will be changed by system after viewWillAppear. So my solution is:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.automaticallyAdjustsScrollViewInsets = NO;
    
        /// any other codes
    }
    
    - (void)viewWillLayoutSubviews {
        self.tableView.contentInset = UIEdgeInsetsMake(self.topLayoutGuide.length, 0, 0, 0);
    }
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    
        // change tableView data source
    
        [self.tableView reloadData];
         NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.dataSourceArray count] - 1 inSection:0];
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];
    }
    
    0 讨论(0)
  • 2020-12-30 01:56

    I ran into another issue (probably a bug) with scrollToRowAtIndexPath specifically on an iPhone X running ios11. My table has a few hundred sections and in collapsed mode ~10 would fit in the visible screen. As the indexPath got deeper, the scrolling gradually fell behind.

    For example, when I wanted the search to find the item in row 30, the ScrollPositionTop would have one additional row before the actual row I expect to be at the top.

    And as I tested searching for deeper rows, it started falling behind even more where for say anything past 100 rows deep or so, the expected row did not even come in the visible area.

    The workaround I found so far is to say animated:NO for the scrolling within dispatch_async, then it works without any glitches.

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