如何解决如何在 Swift 中完成所有单元格数据加载之前不显示 tableView
我有一个 tableView,但是当它加载单元格时,它看起来很难看,因为图像需要一两秒钟才能加载。我有一个要显示的加载器,然后当所有单元格加载完毕后,我想显示tableView 和隐藏加载程序。如何判断 tableView 何时完成加载并显示 tableView?
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell",for: indexPath) as! MainTableViewCell
let payment = self.payments[indexPath.row]
if let profileImageUrl = payment.picture {
cell.profilePicture.loadImageUsingCacheWithUrlString(profileImageUrl)
cell.profilePicture.layer.cornerRadius = cell.profilePicture.frame.size.width / 2
cell.profilePicture.clipsToBounds = true
}
if payment.message == "none" {
cell.detailsLabel.text = "No Message"
} else {
cell.detailsLabel.text = "\"\(payment.message ?? "")\""
}
cell.amountLabel.textColor = UIColor.init(red: 105/255,green: 105/255,blue: 105/255,alpha: 1)
cell.amountLabel.text = "$\(payment.amount ?? "")"
return cell
}
解决方法
所以基本上您想显示 Activity Indicator
以警告/显示您的用户等待一段时间直到可见单元格正确加载,对吗?
您有自己的自定义加载器 (Activity Indicator
),希望在所有单元格加载完毕后显示您编写的内容。
这里需要了解的重要一点是,UITableView
不会加载所有单元格,因为它不能那样工作。
无论您有多少项,表格视图都只会使用 tableView:cellForRowAtIndexPath:
创建 6 或 7 个可见的单元格(取决于)。
如果用户滚动,则会为新的可见单元格再次调用上述方法。
这意味着
表格从未真正完成加载。
回答关于如何检测表格是否完成加载可见单元格的问题,那么您可以尝试这段代码:
override func tableView(_ tableView: UITableView,willDisplay cell: UITableViewCell,forRowAt indexPath: IndexPath) {
if let lastVisibleIndexPath = tableView.indexPathsForVisibleRows?.last {
if indexPath == lastVisibleIndexPath {
// turn off/hide your loader
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。