在iOS Swift中预取UITableView

UITableViewDataSourcePrefetching :iOS 10中的一种协议UITableView,可以异步加载异步数据源。 用户不希望滚动到chhmhoặcđừ滚动s并加载nhữngindexPathtiếptheosắpđượchiệnlên,请确保平滑滚动cho tableviewhoặccollectionview。

Cácbướcthựchiện:

  • TableviewVà数据源
  • 符合协议
  tableView   prefetchDataSource = 自我 
  • 可以通过通讯协议tableView(_:prefetchRowsAt:)加载数据,并可以缓存。
  • 显示数据UI UI tableView(_:cellForRowAt:)
  • 取消加载获取异步任务: tableView(_:cancelPrefetchingForRowsAt:)

生活圈:

  • Khi lnnđầhi hinn单元格,tindex索引路径= 0,hàmprefetchRowsAt索引10索引ti p theotínhtừ索引cuốiđhiệntrênmànhình(内容偏移量)。
  • Khi用户滚动chậmxuống,hàmprefetchRowsAtsẽtrảvề1索引tiếptheo,tứclà索引thứ11(10 +1)。
  • Khi用户滚动滚动索引,滚动索引10索引索引滚动索引(内容偏移量)。
  • Khi用户滚动chậmsauđóđộtngộtđổihướng滚动,hàmprefetchRowsAtsẽtrảvề10 index theohướngđãđổi,đồngthờisẽvàohàmcancelPrefetchingForRowsAttrảvềindexàvđindexà thị。

代码:

-Thựchiệnkhaibáo代表

  myTable.prefetchDataSource =自我 

-实施代表

  func tableView(_ tableView:UITableView, prefetchRowsAt indexPaths:[IndexPath]){print(“ Fetching:\(indexPaths)”)//异步加载:数据,图像... 
func tableView(_ tableView:UITableView, cancelPrefetchingForRowsAt indexPaths:[IndexPath]){print(“ Fetching Cancel:\(indexPaths)”)//取消隐藏các操作异步加载数据
}

您可以跟随đểnhậnnhữngbàiviếtmớinhấtcủamìnhnhé。