Iva*_*ino 14 uitableview uiactivityindicatorview ios swift3
我想在显示最后一个单元格时在我的UITableView底部添加一个ActivityIndicator,而我正在获取更多数据,然后在获取数据时隐藏它.
所以我滚动到底部 - >显示的最后一行 - > spinner在获取数据时开始旋转 - >获取数据,隐藏微调器 - >添加到tableview的新数据.
关于如何实现这一目标的任何提示?
谢谢 ;)
Aya*_*bar 52
添加此功能
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastSectionIndex = tableView.numberOfSections - 1
let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
if indexPath.section == lastSectionIndex && indexPath.row == lastRowIndex {
// print("this is the last cell")
let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
spinner.startAnimating()
spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))
self.tableview.tableFooterView = spinner
self.tableview.tableFooterView?.isHidden = false
}
}
Run Code Online (Sandbox Code Playgroud)
和tableFooterView应该在数据加载时隐藏.
斯威夫特4
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastSectionIndex = tableView.numberOfSections - 1
let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
if indexPath.section == lastSectionIndex && indexPath.row == lastRowIndex {
// print("this is the last cell")
let spinner = UIActivityIndicatorView(style: .gray)
spinner.startAnimating()
spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))
self.tableview.tableFooterView = spinner
self.tableview.tableFooterView?.isHidden = false
}
}
Run Code Online (Sandbox Code Playgroud)
我使用了@Ayaz Akbar 的解决方案,它很有魅力。它只是需要一些改进。这是我如何根据我的需要采用它。
如果没有更多项目要加载,则删除加载指示器
由于您每次到达项目末尾时都会添加加载指示器,因此您将不再向表视图数据源添加新项目。此时您需要移除加载指示器。我正在使用一个自定义UITableViewDataSource,每次收到新数据时都会调用它的完成处理程序。我还保留了一个var newCount来跟踪新项目的数量。所以这是我在视图控制器中隐藏活动指示器的操作:
private lazy var dataSource: CustomTableViewDataSource = {
return CustomTableViewDataSource(query: CustomQuery) { [unowned self] (result) in
// For the initial load I needed a standard activity indicator in the center of the screen
self.stopMainActivityIndicatorIfNeeded()
switch result {
case .success(()):
if self.dataSource.newCount > 0 {
self.tableView.reloadData()
} else {
// Hide the tableFooterView, respectively the activity indicator
self.tableView.tableFooterView = nil
}
case .failure(let error):
self.showError(error)
}
}
}()
Run Code Online (Sandbox Code Playgroud)
这是我更新的UITableView委托方法:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastSectionIndex = tableView.numberOfSections - 1
let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
if indexPath.section == lastSectionIndex && indexPath.row == lastRowIndex && dataSource.newCount > 0 {
let spinner = UIActivityIndicatorView(style: .gray)
spinner.frame = CGRect(x: 0.0, y: 0.0, width: tableView.bounds.width, height: 70)
spinner.startAnimating()
tableView.tableFooterView = spinner
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14153 次 |
| 最近记录: |