【问题标题】:How to not show tableView until all cell data is finished loading in Swift如何在 Swift 中完成所有单元格数据加载之前不显示 tableView
【发布时间】:2021-02-15 17:20:35
【问题描述】:

我有一个 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
    }

【问题讨论】:

  • 下载时应该有微调器的SDWebImage(或其他KingFisher、Alamofire+Image)怎么样:github.com/SDWebImage/SDWebImage/wiki/…
  • 我有一个我想使用的自定义加载器,在我的屏幕中间...
  • 你想下载所有的图片,然后用加载的图片重新加载 tableView?寻找 DispatchGroup,进入,离开,然后通知。

标签: ios swift xcode tableview cell


【解决方案1】:

所以基本上你想显示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
                }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多