深入探究 UITableView 与 UICollectionView 的使用
1. UITableView 的使用
1.1 数据预取
若要实现数据预取功能,可让 ViewController
遵循 UITableViewDataSourcePrefetching
协议:
extension ViewController: UITableViewDataSourcePrefetching {
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
}
}
这里传入的 indexPaths
是一个 IndexPath
数组,这与单个 IndexPath
不同,它允许对一组单元格进行批量处理。当单元格数据需要异步获取(如图像或实时数据)时,这种批量处理方式非常有用。
1.2 单元格选择
当用户点击单元格时,若要显示一个提醒框,可在 ViewController.swift
的扩展中添加如下代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {