iOS tableView scrolls to the specified row / segment and bug Guide

Select cell to scroll to the specified location

It's very simple, one sentence to finish:

/* 
    The most recently selected cell scrolls to the specified location:
    UITableViewScrollPositionNone, 
    UITableViewScrollPositionTop, // Top
    UITableViewScrollPositionMiddle, // Middle
    UITableViewScrollPositionBottom// bottom
*/
[self.tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];

/** Scroll to top */
[self.tableView setScrollsToTop:YES];
    
/** Scrolls the specified region to the visible region, which is not valid if it is already in the visible region */
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 100, 100) animated:YES];

Scroll to the specified IndexPath

The same is just one more parameter than the previous method: indexPath. Anyone who has used tableView should be familiar with this.

/* Scroll the specified row of the specified segment to the specified location*/
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

Scroll to the specified location by setting the content offset of ScrollView

This method is more flexible, but it has the most problems. In my project, there are multiple levels of table view nested multi-level directory expansion problems, which are used and some problems arise.

/*Scroll tableView to the specified location by setting contentOffset*/
[self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];

This requires a very important parameter: CGPoint.
So, how can we calculate this position? Don't worry, Apple's UITableView component comes with some powerful API s that we are not familiar with.

/** Specifies the top most rect of the section */
[self.tableView rectForSection:0];
    
/** Specify the rect of the cell */
[self.tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
/** sectionHeader rect of */
[self.tableView rectForHeaderInSection:0];
    
/** sectionFooter rect of */
[self.tableView rectForFooterInSection:0];

In addition, UIView may be used to calculate the location:

/* viewB viewC on vs. frame of viewA */
[viewB convertRect:viewC.frame toView:viewA];

/* viewA viewB on vs. frame of viewC */
[viewC convertRect:viewB.frame fromView:viewA];

Bugs encountered in scrolling and Solutions

  • When ScrollTop appears, a large part of the blank area will slide. The initial value of the estimated sectionheaderheight dynamic calculation property should not be set too high. If it is higher than the actual height, there will be problems in calculation.

  • Be sure to remember that the row and section in the NSIndexPath object must correspond to the data source. Never cross the border! Don't cross the border! Don't cross the border! Important things are to be repeated for 3 times.

  • When rolling, if the rolling range is large, there may be several situations:

    • 1. Too fast, users will be dazzled [cancel scrolling animation beyond a certain threshold]

    • 2. Scroll down. Once the offset has a negative value, it is easy to trigger the drop-down refresh [filter the drop-down refresh according to business needs]

    • 3. There is a blank area due to excessive sliding, and it fails to trigger the pull-down refresh without touching or retracting. [just remove the animation]

Keywords: Mobile

Added by V-Man on Mon, 04 May 2020 19:57:46 +0300