简体   繁体   中英

UITableViewDataSourcePrefetching->prefetchRowsAt always has 0 and 1 in indexPaths array

I have used UITableViewDiffableDataSource as the datasource for my tableView. On initial load, 8 items are fetched from server and displayed. When I try to scroll,

func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {

is called, but indexPaths always has 0 and/or 1. It is never getting incremented to ask for prefetching 10, 11,12, etc rows. What am I missing here?

From Apple Docs:

Table views do not call this method for cells they require immediately, so your data source object must also be able to fetch the data itself.

This approach is offered by the docs:

The tableView( :prefetchRowsAt:) method is not necessarily called for every cell in the table view. Your implementation of tableView( :cellForRowAt:) must therefore be able to cope with the following potential situations:

  • Data has been loaded via the prefetch request, and is ready to be displayed.
  • Data is currently being prefetched, but is not yet available.
  • Data has not yet been requested.

One approach that handles all of these situations is to use Operation to load the data for each row. You create the Operation object and store it in the prefetch method. The data source method can then either retrieve the operation and the result, or create it if it doesn't exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM